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

1 回表示 (過去 30 日間)
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 件のコメント
John D'Errico
John D'Errico 2016 年 7 月 10 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 7 月 10 日
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 件のコメント
Kenneth Lamury
Kenneth Lamury 2016 年 7 月 11 日
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 2016 年 7 月 11 日
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?

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2016 年 7 月 10 日
編集済み: John D'Errico 2016 年 7 月 10 日
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 件のコメント
John D'Errico
John D'Errico 2016 年 7 月 10 日
編集済み: Walter Roberson 2016 年 7 月 10 日
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 2016 年 7 月 10 日
編集済み: Walter Roberson 2016 年 7 月 10 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by