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
Kenneth Lamury
Kenneth Lamury on 11 Jul 2016
Using the following sequence of statements, and being careful as to which format I use in 'textscan', I'm able to read fixed width FORTRAN formats. Caution! If two fields like' 735.374730' exist, then one may need to rewrite those '%7f' field input data as ' 735. .374730' as 'textscan' may assign var1 = 735.374 & var2 = 730. My test setup of 132 reads works excellently in R2016a Student. Now to work on GOTO FORTRAN statements.
fmt_num0=['%7f%7f%7f%7f%7f%7f%3f%3f%3f%3f%2f%24c']; % 6F7.0,4I3,I2,6A4
fmt_num1='%7f%7f%7f%7f%7f%7f%7f%7f%7f%7f'; % 10F7.0
fmt_num1a='%7f%7f%7f%7f';
fmt_num1b='%7f%7f%7f%7f%7f';
fmt_num2='%7f%7f%7f%7f%7f%7f%7f';%7f%7f%7f%2f
fmt_num3='%21c%7f%7f%7f%7f%7f%7f%7f'; % 5A4,A1,7F7.0
fgetl(fid);
XYc=textscan(fid,fmt_num1b,1);
XY=cell2mat(XYc);
COND(N)=XY(1)
FLAP(N)=XY(2)
SF(N)=XY(3)
COTNAC(N)=XY(4)
SHRCRT(N)=XY(5)
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
John D'Errico
John D'Errico on 10 Jul 2016
Edited: Walter Roberson on 10 Jul 2016
This has nothing to do with an understanding of MATLAB. I explained what to do.
  1. Paste your code in.
  2. Select the block of code.
  3. Click on the button "{} Code" There is a link on the web page for exactly that.
Walter Roberson
Walter Roberson on 10 Jul 2016
Edited: 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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!