Undefined command/function 'reshape'.
Mostra commenti meno recenti
I have a script I am trying to write to call a function (Not written by me) to extract files from a folder. The function basically takes data from a folder and then places them into a struct and reformats it. I am not able to really visualize what the finished result would look like since it is getting stuck on an error.
clc
clear all
close all
files = dir('/Users/admin/Documents/MATLAB/pivdat/*.v3d');
for k = 1:length(files)
[h,d] = svecread(files(k).name ,1,8)
end
Then I have parts of the actual function here:
function [varargout] = svecread(varargin)
% Inputs:
msg = nargchk(1,3,nargin); if ~isempty(msg), error(msg), end;
% Defaults:
if nargin < 3
varargin{3} = 8; % default columns value (13/08/01)
if nargin < 2
varargin{2} = 1; % default number of header lins
end
end
% Assign variables
name = varargin{1};
comments = varargin{2};
columns = varargin{3};
disp(name);
% Extension issue
if isempty(findstr(lower(name),'.v3d')), name = strcat(name,'.v3D'); end;
% Read the file
fid=fopen(name,'r');
if fid<0
error('File not found');
end
[dch,count]=fread(fid,inf,'uchar');
fclose(fid);
% Reformat the data
chdat=[dch(:)',setstr(13)];
ind10=find(chdat==setstr(10));
comp=computer;
if strcmp(comp(1:3),'PCW')|strcmp(comp(1:3),'VAX')|strcmp(comp(1:3),'ALP'),
% replace cr-lf with cr only for PC's, VAXen and Alphas
chdat(ind10)=setstr(' '*ones(1,length(ind10)));
else
%replace line-feeds with carriage-returns for Unix boxes
chdat(ind10)=setstr(13*ones(length(ind10),1));
end
% Now replace commas with spaces
indcom=find(chdat==',');
chdat(indcom)=setstr(' '*ones(1,length(indcom)));
%find carriage-returns
ind13=find(chdat==setstr(13));
% Truncate array to just have data
if comments==0,
char1=1;
else
char1=ind13(comments)+1;
end
hdr = lower(chdat(1:char1-1));
chdata=chdat(char1:count);
% Convert it
data=sscanf(chdata,'%g',[columns inf])';
% Find and remove bad points > 9.99e9
badind = find(data>9e9);
if ~isempty(badind), data(badind) = 0; warning(sprintf('Bad %d points',length(badind))); end;
% Parse the header
i = findstr(hdr,'i=');
j = findstr(hdr,'j=');
k = findstr(hdr,'k='); % 3rd dimension index, 19/08, Alex.
[i,junk] = strtok(hdr(i+2:end));
[j,junk] = strtok(hdr(j+2:end));
[k,junk] = strtok(hdr(k+2:end)); % 19/08/01
i = eval(i); j = eval(j); k= eval(k); % 19/08/01
disp(i);
disp(j);
disp(k);
disp(columns);
data = reshape(data,[i,j,columns]);
data = permute(data,[2 1 3]);
When I run this I receive the result:
Undefined command/function 'reshape'.
Error in svecread (line 126)
data = reshape(data,[i,j,columns]);
Error in pivdatareadertest (line 7)
[h,d] = svecread(files(k).name ,1,8)
I did not alter the file except for making it display i, j, and columns, to ensure they are actually outputting values. Having read some other forum questions and also checking the reshape function page for matlab, I tried removing the brackets all together, keeping the brackets and removing the commas, also tried only putting brackets around i,j. I am quite stuck on what might be causing this. I assume Reshape is a built in function and so I am not missing any additional libraries? I know the data was loaded properly as I made it display that too and it did just fine prior to reshape.
Any help would be very much appreciated.
15 Commenti
That function has a rather strong code smell, it is not surprising that it throws strange errors (which may be due to those evil EVALs). If you upload a sample data file we can probably do much better.
Please also show the complete output of this command:
which reshape -all
Cris LaPierre
il 10 Apr 2024
The error is not reproducible on my system. Please run the following command and share the result
which reshape
Try this first to see if it gives you the correct result:
reshape(1:24, [2,3,4])
Aquatris
il 10 Apr 2024
You probably removed the path of reshape function from Matlab paths.
Jensen Lam
il 11 Apr 2024
Jensen Lam
il 11 Apr 2024
Jensen Lam
il 11 Apr 2024
The variable i is a string.
i =
"0.000035"
j =
54
columns =
8
Consequently, concatenating it with j and columns yields a string array, resulting in no appropriate method for reshape() being found. Seems like a bit of a misleading error message. I would have figured that it would have used the method for double numeric inputs and then thrown an error due to the second parameter being invalid.
EIther way, the size inputs need to be numeric and integer. The variable i is neither. The variables j and columns are both numeric and integer, but numel(data) is not integer-divisible by their product.
FWIW, these are the only integer factor triples of numel(data), for the given file:
sz = [1 1 224696;
1 2 112348;
1 4 56174;
1 8 28087;
2 2 56174;
2 4 28087];
I haven't sussed out your intended output shape, but recall that you can always specify a slack dimension using the scalar syntax.
x = reshape(x,[],cols,pages);
That might help avoid the need to extract one of those parameters.
Jensen Lam
il 11 Apr 2024
Jensen Lam
il 11 Apr 2024
Jensen Lam
il 11 Apr 2024
Modificato: Jensen Lam
il 11 Apr 2024
Jensen Lam
il 11 Apr 2024
DGM
il 11 Apr 2024
At this point, I'm not yet worried about updating the string extraction part. I'm just trying to figure out how numel(data) is supposed to be factored. We have ostensible sizes given by 73, 54, 1, and 8, but only 1 and 8 are factors of numel(data). Might want to make sure that data is the correct length. I'm also curious as to why j = 54 and there are 53 named variables. Maybe there's a column for a timestamp or something?
Obviously, I'm not familiar with this file format or what to expect of variations in header formatting, but I note that the header is on one line (the decoder expects this), and this particular file has 3943 non-header lines (one of which is empty), each of which appears to contain 57 fields. How that relates to the numbers in the header, I have no idea.
EDIT: Oh. 73x54 = 3942, and there are only 8 of the 57 fields which appear to be used. Not sure what information should be used to find the number of fields.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Workspace Variables and MAT Files in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
