MATLAB Answers

After using this sequence of statements several times, why am I getting an error message?

1 view (last 30 days)
This sequence of statements has worked numerous times in my program, but now gives an error message. I'm 49 lines into my fixed format FORTRAN input data when it suddenly stopped. Can anyone see my error?
fmt_num1='%7f%7f%7f%7f%7f%7f%7f%7f%7f%7f';
if L <= 10
fgetl(fid); %Get a new line of data
XYc=textscan(fid,fmt_num1,1); %Read the input (cell?) data per format
XY=cell2mat(XYc); %Convert cell data into matrix values; Line 232 in MATLAB
for i=1:L
ALSP(i)=XY(i);
end
end
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 75)
m{n} = cat(2,c{n,:});
Error in CNSMTX (line 232)
XY=cell2mat(XYc) %Line 232 in MATLAB
  1 Comment
John D'Errico
John D'Errico on 10 Jul 2016
LEARN TO FORMAT YOUR CODE WHEN YOU PASTE IT IN.
paste it in, then select it, then click on the "{} Code" button. Not hard. But it makes your code readable.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jul 2016
You probably have at least one missing value that could not be handled by reading on to additional lines because there was a format mismatch, and you did not ask for missing value handling (missing value handling really only works correctly if you use a field Delimiter though.)
I looked at your data before, and you are not going to be able to parse the lines using numeric formats. You need to do what I wrote before: break the line up into fixed width fields of characters and then convert the characters to numeric.
  4 Comments
Walter Roberson
Walter Roberson on 11 Jul 2016
Using %c formats and converting to numeric later would not have those problems.
Your data does have columns that run together. You are going to continue to have problems until you switch to extracting substrings and converting those; I have shown you some different approaches for that. Why do you insist on hacking around the problem when you can do it right without much difficulty?

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 10 Jul 2016
Edited: John D'Errico on 10 Jul 2016
READ THE ERROR MESSAGE! God gave us error messages so that we could read them. Ok, Cleve did. Same thing wrt MATLAB.
Some lines in your file do not have the same number of elements, but you are trying to merge them all into one flat array. Arrays in MATLAB all must have the same number of elements in each row. They are rectangular. Your file is apparently not.
And learn to use proper formatting for your code when you paste it in. I had to do that myself just to read what you had done.
  4 Comments
Walter Roberson
Walter Roberson on 10 Jul 2016
Using # (followed by a space) at the beginning of a chunk can be used to make the chunk a numbered list. Each "# " at the beginning of a line advances to the next number. Empty line ends the numbered list. If you use "* " instead of "# " then the list is unnumbered.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!