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
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
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 个评论
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
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
2016-7-10
编辑:Walter Roberson
2016-7-10
This has nothing to do with an understanding of MATLAB. I explained what to do.
- Paste your code in.
- Select the block of code.
- Click on the button "{} Code" There is a link on the web page for exactly that.
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 Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!