MATLAB Answers

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

2 views (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

Show 1 older comment
Walter Roberson
Walter Roberson on 10 Jul 2016
I do not recommend putting in delimiters; I recommend that you break the line up into chunks of appropriate width and then convert the chunks up. It is a simple change to your code:
fmt_num1='%7c%7c%7c%7c%7c%7c%7c%7c%7c%7c';
if L <= 10
fgetl(fid); %Get a new line of data
XYc = textscan(fid,fmt_num1,1); %Read the input (cell?) data per format
ALSP = str2double(XYc);
end
I might have misunderstood the role of your L. If you need to read 10 of these lines then probably something closer to
num1_widths = [7, 7, 7, 7, 7, 7, 7, 7, 7, 7];
num1_perline = length(num1_widths);
num1_twidth = sum(fmt_num1_widths);
for L = 1 : 10
tline = fgetl(fid);
if ~ischar(tline)
error('unexpected end of file');
end
XYc = mat2cell(tline, 1, [num1_widths, num1_twidth-length(tline)]); %the last cell is to deal with any extra characters
ALSP(L,:) = str2double(XYc(1:num1_perline));
end
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

Show 1 older comment
Walter Roberson
Walter Roberson on 10 Jul 2016
When you are creating a Question or a response:
The Answers system looks at chunks of consecutive non-empty lines. If the first line in the chunk starts with at least one blank then the entire chunk will be treated as code and will be displayed to the readers in monospaced font with line breaks exactly as entered. But if the first line in the chunk is not whitespace, then the entire chunk will be treated as narrative text that will be shown to the reader with proportionally spaced font and will be "flowed" to have as many words as fit the line.
When you want to put code in to an Answer or response, you should leave at least one empty line before and after it. Then use your mouse to select the code, and then click the '{} Code' button. The editor system will then indent the text, which the system knows means that it should be treated as code.
Do not put in actual { or } characters unless they are part of the code itself: use the '{} Code' button that is between the font selection at the left side and the attachment buttons on the right side (the link symbol, the picture frame symbol, the paperclip symbol.)
Do not do this for descriptive text: only do it for code (or for things you want to quote from a reference source or otherwise should not be automatically reformatted.)
John D'Errico
John D'Errico 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
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!

Translated by