Undefined command/function 'reshape'.

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

Stephen23
Stephen23 il 10 Apr 2024
Modificato: Stephen23 il 10 Apr 2024
@Jensen Lam: please click the paperclip button to upload a sample data file.
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
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])
ans =
ans(:,:,1) = 1 3 5 2 4 6 ans(:,:,2) = 7 9 11 8 10 12 ans(:,:,3) = 13 15 17 14 16 18 ans(:,:,4) = 19 21 23 20 22 24
You probably removed the path of reshape function from Matlab paths.
@Fangjun Jiang It displayed those answers
@Stephen23 Here is the output I received :
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@cell/reshape) % cell method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@char/reshape) % Shadowed char method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@double/reshape) % Shadowed double method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@int16/reshape) % Shadowed int16 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@int32/reshape) % Shadowed int32 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@int64/reshape) % Shadowed int64 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@int8/reshape) % Shadowed int8 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@logical/reshape) % Shadowed logical method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@opaque/reshape) % Shadowed opaque method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@single/reshape) % Shadowed single method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@struct/reshape) % Shadowed struct method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@uint16/reshape) % Shadowed uint16 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@uint32/reshape) % Shadowed uint32 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@uint64/reshape) % Shadowed uint64 method
built-in (/Applications/MATLAB_R2019b.app/toolbox/matlab/elmat/@uint8/reshape) % Shadowed uint8 method
reshape is a built-in method % Shadowed string method
reshape is a built-in method % Shadowed meta.PackageList method
reshape is a built-in method % Shadowed handle method
reshape is a built-in method % Shadowed meta.ClassList method
reshape is a built-in method % Shadowed meta.FunctionList method
reshape is a built-in method % Shadowed meta.TypeList method
reshape is a built-in method % Shadowed matlab.mixin.internal.indexing.Paren method
reshape is a built-in method % Shadowed matlab.mixin.internal.indexing.ParenAssign method
reshape is a built-in method % Shadowed MException method
reshape is a built-in method % Shadowed matlab.internal.language.ir_workspace_ownership_proxy method
reshape is a built-in method % Shadowed matlab.mixin.internal.MatrixDisplay method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.Printable method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.Windowable method
reshape is a built-in method % Shadowed matlab.ui.Figure method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.AutoResizable method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.AxesLimits method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.Legacy method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.KeyInvokable method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.Positionable method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.Scrollable method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.Selectable method
reshape is a built-in method % Shadowed matlab.ui.internal.mixin.TerminalStateRepresentable method
reshape is a built-in method % Shadowed matlab.ui.Root method
reshape is a built-in method % Shadowed matlab.ui.control.Component method
reshape is a built-in method % Shadowed matlab.ui.container.Container method
reshape is a built-in method % Shadowed matlab.ui.container.CanvasContainer method
reshape is a built-in method % Shadowed matlab.ui.control.WebComponent method
reshape is a built-in method % Shadowed matlab.graphics.Graphics method
reshape is a built-in method % Shadowed matlab.graphics.GraphicsPlaceholder method
reshape is a built-in method % Shadowed matlab.graphics.GraphicsDisplay method
reshape is a built-in method % Shadowed matlab.graphics.mixin.internal.Copyable method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsPropertyHandler method
reshape is a built-in method % Shadowed matlab.graphics.internal.ReferenceObject method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsMetaProperty method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsMetaClass method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsJavaVisible method
reshape is a built-in method % Shadowed matlab.graphics.mixin.Mixin method
reshape is a built-in method % Shadowed matlab.graphics.mixin.NodeChildren method
reshape is a built-in method % Shadowed matlab.graphics.mixin.NodeParent method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsBaseFunctions method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsUIProperties method
reshape is a built-in method % Shadowed matlab.graphics.internal.GraphicsCoreProperties method
reshape is a built-in method % Shadowed matlab.graphics.internal.Exportable method
reshape is a built-in method % Shadowed meta.class method
reshape is a built-in method % Shadowed meta.MetaData method
reshape is a built-in method % Shadowed matlab.mixin.Heterogeneous method
reshape is a built-in method % Shadowed matlab.mixin.SetGet method
reshape is a built-in method % Shadowed hgsetget method
reshape is a built-in method % Shadowed dynamicprops method
reshape is a built-in method % Shadowed matlab.mixin.CustomDisplay method
reshape is a built-in method % Shadowed matlab.mixin.internal.CompactDisplay method
reshape is a built-in method % Shadowed meta.property method
reshape is a built-in method % Shadowed matlab.mixin.internal.Scalar method
reshape is a built-in method % Shadowed matlab.internal.CycleOwnerProxy method
reshape is a built-in method % Shadowed JavaVisible method
reshape is a built-in method % Shadowed matlab.mixin.internal.DefaultFactoryPropHandler method
reshape is a built-in method % Shadowed internal.deviceplugindetection.Manager method
reshape is a built-in method % Shadowed internal.hotplug.EventSource method
reshape is a built-in method % Shadowed inputParser method
reshape is a built-in method % Shadowed asyncio.Channel method
reshape is a built-in method % Shadowed asyncio.MessageHandler method
reshape is a built-in method % Shadowed asyncioimpl.Channel method
reshape is a built-in method % Shadowed asyncioimpl.InputStream method
reshape is a built-in method % Shadowed asyncioimpl.OutputStream method
reshape is a built-in method % Shadowed asyncioimpl.MessageHandler method
reshape is a built-in method % Shadowed asyncio.InputStream method
reshape is a built-in method % Shadowed asyncio.Stream method
reshape is a built-in method % Shadowed asyncio.OutputStream method
reshape is a built-in method % Shadowed matlab.internal.language.localized_anonymous_function_handle_workspace_ownership_proxy method
reshape is a built-in method % Shadowed event.listener method
reshape is a built-in method % Shadowed containers.Map method
reshape is a built-in method % Shadowed meta.EnumeratedValue method
reshape is a built-in method % Shadowed matlab.unittest.meta.class method
reshape is a built-in method % Shadowed matlab.unittest.meta.method method
reshape is a built-in method % Shadowed matlab.unittest.meta.property method
reshape is a built-in method % Shadowed meta.method method
reshape is a built-in method % Shadowed meta.package method
reshape is a built-in method % Shadowed matlab.internal.i18n.locale method
reshape is a built-in method % Shadowed message method
reshape is a built-in method % Shadowed onCleanup method
reshape is a built-in method % Shadowed matlab.mixin.Copyable method
reshape is a built-in method % Shadowed matlab.internal.language.nested_function_handle_ownership_proxy method
reshape is a Java method % Shadowed com.mathworks.mde.array.ArrayEditor method
reshape is a Java method % Shadowed com.mathworks.mde.desk.PlotableClientBase method
reshape is a Java method % Shadowed com.mathworks.widgets.desk.DTClientBase method
reshape is a Java method % Shadowed com.mathworks.mwswing.MJPanel method
reshape is a Java method % Shadowed javax.swing.JPanel method
reshape is a Java method % Shadowed javax.swing.JComponent method
reshape is a Java method % Shadowed java.awt.Container method
reshape is a Java method % Shadowed java.awt.Component method
/Applications/MATLAB_R2019b.app/toolbox/matlab/bigdata/@tall/reshape.m % Shadowed tall method
/Applications/MATLAB_R2019b.app/toolbox/matlab/datatypes/categorical/@categorical/reshape.m % Shadowed categorical method
/Applications/MATLAB_R2019b.app/toolbox/symbolic/symbolic/@sym/reshape.m % Shadowed sym method
/Applications/MATLAB_R2019b.app/toolbox/shared/controllib/engine/@InputOutputModel/reshape.m % Shadowed InputOutputModel method
/Applications/MATLAB_R2019b.app/toolbox/shared/rotations/rotationslib/@quaternion/reshape.m % Shadowed quaternion method
@Stephen23 Here is the link to the file, it expires Friday thank you v3d file cant be uploaded here: FSTwentyHz000001.T000.D000.P000.H000.PIV.v3d
DGM
DGM il 11 Apr 2024
Modificato: DGM 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.
@DGM You are right. "i" is definitely causing the problem. I am finding it difficult to imagine what the intended output would look like as well. Doesn't help that this function was written in 2001. Im guessing the 0.000035 was not an intended value to be calculated and maybe theres something outdated in there causing the wrong value of i to be calculated. Further more looking into 'findstr' apparently Matlab does not recommend using it
Just putting a bunch of disp(i), disp(j), disp(k), disp(columns) everywhere it seems that after :
i = findstr(hdr,'i=');
j = findstr(hdr,'j=');
k = findstr(hdr,'k=');
j and k are a single integer, whereas i gives two values. And this process:
[i,junk] = strtok(hdr(i+2:end));
[j,junk] = strtok(hdr(j+2:end));
[k,junk] = strtok(hdr(k+2:end));
Is what turns i into "0.000035", j into 54, and k into 1. Hmmmmmm
DGM
DGM il 11 Apr 2024
Modificato: DGM il 11 Apr 2024
It looks like part of the problem is due to multiple matches for 'i=' within hdr.
fieldlambdaci="0.000035"
and
i=73
Still, the math doesn't work out.
There are 53 variables which are named in the header.
Jensen Lam
Jensen Lam il 11 Apr 2024
Modificato: Jensen Lam il 11 Apr 2024
title="c:\experiments11\experiment stereopivclassfour\fstwentyhz\analysis\fstwentyhz000001.t000.d000.p002.h000.piv.v3d" variables="x mm" "y mm" "z mm" "u m/s" "v m/s" "w m/s" "chc" "residual error" "u turbulent fluctuation m/s" "v turbulent fluctuation m/s" "w turbulent fluctuation m/s" "rate of strain 1/s" "vorticity 1/s" "q criterion 1/s2" "lambda ci 1/s" "velocity magnitude m/s" "du/dx 1/s" "dv/dy 1/s" "du/dy 1/s" "dv/dx 1/s" "u mean run/avg m/s" "u mean run/avg uncertainty m/s" "v mean run/avg m/s" "v mean run/avg uncertainty m/s" "w mean run/avg m/s" "w mean run/avg uncertainty m/s" "u std dev run/avg m/s" "u std dev run/avg uncertainty m/s" "v std dev run/avg m/s" "v std dev run/avg uncertainty m/s" "w std dev run/avg m/s" "w std dev run/avg uncertainty m/s" "velocity magnitude mean run/avg m/s" "velocity magnitude std dev run/avg m/s" "normal reynolds stress x run/avg m2/s2" "normal reynolds stress x run/avg uncertainty m2/s2" "normal reynolds stress y run/avg m2/s2" "normal reynolds stress y run/avg uncertainty m2/s2" "normal reynolds stress z run/avg m2/s2" "normal reynolds stress z run/avg uncertainty m2/s2" "shear reynolds stress xy run/avg m2/s2" "shear reynolds stress xy run/avg uncertainty m2/s2" "shear reynolds stress xz run/avg m2/s2" "shear reynolds stress xz run/avg uncertainty m2/s2" "shear reynolds stress yz run/avg m2/s2" "shear reynolds stress yz run/avg uncertainty m2/s2" "average kinetic energy run/avg m2/s2" "average kinetic energy run/avg uncertainty m2/s2" "turbulent kinetic energy run/avg m2/s2" "turbulent kinetic energy run/avg uncertainty m2/s2" "u turbulence intensity run/avg %" "u turbulence intensity run/avg uncertainty %" "v turbulence intensity run/avg %" "v turbulence intensity run/avg uncertainty %" "w turbulence intensity run/avg %" "w turbulence intensity run/avg uncertainty %" "total turbulence intensity run/avg %" datasetauxdata application="stereopiv" datasetauxdata micrometersperpixelx="66.475067" datasetauxdata micrometersperpixely="66.475067" datasetauxdata micrometersperpixelz="1.000000" datasetauxdata lengthunit="mm" datasetauxdata origininimagex="0.000000" datasetauxdata origininimagey="0.000000" datasetauxdata origininimagez="0.000000" datasetauxdata microsecondsperdeltat="15000.000000" datasetauxdata timeunit="ms" datasetauxdata secondarypeaknumber="0" datasetauxdata dewarpedimagesource="0" datasetauxdata containsstatistics="1" datasetauxdata fieldumean="0.014685" datasetauxdata fieldvmean="-0.000076" datasetauxdata fieldwmean="-0.001179" datasetauxdata fieldvelocitymagnitude="0.018831" datasetauxdata fielduturbulentfluctuation="0.000308" datasetauxdata fieldvturbulentfluctuation="-0.000100" datasetauxdata fieldwturbulentfluctuation="-0.001927" datasetauxdata fieldustddev="0.003919" datasetauxdata fieldvstddev="0.001117" datasetauxdata fieldwstddev="0.013472" datasetauxdata fieldvelocitymagnitudestddev="0.007778" datasetauxdata fielddudx="0.000067" datasetauxdata fielddudy="0.000088" datasetauxdata fielddvdx="-0.000004" datasetauxdata fielddvdy="-0.000010" datasetauxdata fieldrateofstrain="0.000042" datasetauxdata fieldvorticity="-0.000093" datasetauxdata fieldqcriterion="0.000000" datasetauxdata fieldlambdaci="0.000035" zone solutiontime=0.000000 i=73 j=54 k=1 f=point
^^^^^^ i is definitely getting caught on the 0.000035 where I bolded, I think the intended value is 73 for this specific file! How do I make it so that it catches i = instead of the i attached to the end of fieldmabda?
Edit: The difference is actually just a space lol. so findstr('hdr', ' i = ') should do it. But now I am getting new errors:
[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
Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for
equality, use '=='
Great.
Here is the output:
=73
54
1
Why is it reading the equal sign along with the 73? I haven't got a clue
Sorry for the many posts.
[i,junk] = strtok(hdr(i+3:end));
instead of th3 2 fixed the indexing and removed the "=". But yes you are right. Math doesn't work out and I can't reshape still
DGM
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?
DGM
DGM il 11 Apr 2024
Modificato: DGM il 11 Apr 2024
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.

Accedi per commentare.

 Risposta accettata

DGM
DGM il 11 Apr 2024
Modificato: DGM il 11 Apr 2024
Again, I have no idea what variations might exist between files. I also don't really know how far you want to go with changing things. This seems to work, but relies on a lot of things which are relatively new (compared to 2001-2008)
name = 'FSTwentyHz000001.T000.D000.P000.H000.PIV.v3d';
headerlines = 1;
columns = 8;
% this is only needed for the forum
unzip([name '.zip']);
% append an extension if the user neglected to include it
% this seems like it would cause as many problems as it might fix.
if isempty(findstr(lower(name),'.v3d'))
name = strcat(name,'.v3D');
end
% Read the header
fid = fopen(name,'r');
if fid<0
error('File not found');
end
hdr = fgetl(fid);
fclose(fid);
% Parse the header
% i'm terrible at using regex safely,
% so take this with a grain of salt
hdr = lower(hdr);
i = regexp(hdr,'i=(\d+)','tokens');
i = str2double(i{1}{1});
j = regexp(hdr,'j=(\d+)','tokens');
j = str2double(j{1}{1});
k = regexp(hdr,'k=(\d+)','tokens');
k = str2double(k{1}{1});
% read everything using a convenience tool
data = readmatrix(name,'filetype','text', ...
'expectednumvariables',columns, ...
'numheaderlines',headerlines);
% then reshape
% i don't know which way this is supposed to be oriented
data = reshape(data,i,j,[]);
data = permute(data,[2 1 3]);
% it appears to be sensible
imagesc(data(:,:,4))
In this case, I'm just being lazy and using readmatrix() to avoid all the reformatting and ambiguity regarding the number of fields per line. It can still be done, but you'll have to come up with a way to discern how many fields/columns are actually in the file (57) so that sscanf() reads everything right. After that, you can just truncate to 8 if desired.
Note also that we read k. I don't know when it would be used.

2 Commenti

Hey, I really appreciate you helping me with this.
To give a bit of background the data is regarding the velocity of particles in an image taken during flow analysis for aerodynamics. Considering this is freestream data, aka all the particles should just be moving in one direction without much change in axis, your plot makes sense.
As for the "discern how many fields/columsn there are in the file", do you mean that there are currently 57 stated columns, but it is possible that MATLAB mistakenly recognized some as columns whereas they actually aren't/ failed to recognize some columns whereas there should be? And that by actually individually distinguishing each of the headers I will be able to go forth and just truncate to 8 and try it with the old code again?
To be quite honest with you my matlab experience has been mostly computational and not really on the file handling and manipulation of data and structs so I am not exactly sure what the difference between your method and their's would be.
DGM
DGM il 11 Apr 2024
Modificato: DGM il 11 Apr 2024
In the original code, you have a variable called columns, which ends up becoming the number of pages in the output. In the call to sscanf(), this is also used to scan the data as if it had 8 columns.
The problem is that each line in the example file actually has 57 columns (or fields or whatever you want to call em (pun unintended)).
-71.031464, 77.468857, 0.000000, 0.000000, 0.000000, 0.000000, -3, 0.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, 0.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000, -9999999.000000
As a consequence, sscanf() will end up truncating its output to a multiple of 8, when it shouldn't be. The original code doesn't seem to have a way to detect the actual number of columns in the file. You'd need to manually set columns=57, or you'd have to programmatically determine it (e.g. based on the number of delimiters per line).
It appeared to me that there were really only 8 of these columns which have plausible data, so that's what I assumed was intended by the variable called "columns". I assumed we should just read that many columns, and the rest should be ignored. I don't know if that's appropriate. Scrolling deeper into the file, it does look like some of the other columns do contain valid data. Some of them just have some strange extreme values at their border. You might want to look at those and see what you think. It might make more sense to just ignore the presumption of 8 columns, and just change it to
% read everything using a convenience tool
data = readmatrix(name,'filetype','text', ...
'numheaderlines',headerlines);
As far as why the old code was written so very differently, I really am not familiar with revision history older than 2006, since that's when most of the available information stops, but I imagine a big part of the reason why they did it that way was because the available tools were different 20 years ago. I don't even know if regexp() was available in 2001.
Readmatrix() wasn't introduced until R2019a. It can take care of splitting data into lines and fields based on characters which are either determined by the user or automatically.
FWIW, I'm not really great at handling files, but sometimes one strikes me as a puzzle.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 10 Apr 2024

Modificato:

DGM
il 11 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by