What am I not seeing here
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
Here is a small script that I wrote after I missed a colon and saw some unexpected (by me) behavior in some work I was doing.
clc;
a=10;
for i=1length(a) % yes, it is i=<digit 1><no space><length(a)>
disp(i)
end
Yes, I know I am missing the colon in the for loop line, but that is exactly my point. I get this output at the command prompt,
ans =
1
1
but I am expecting an error
??? 1length(a)
|
Error: Unexpected MATLAB expression.
I know I am not able to see something, probably something silly, obvious and embarrassing, but I just don't know what. Would like somebody to help me out here, thanks in advance.
1 件のコメント
Jan
2011 年 6 月 15 日
5 votes in 22 minutes - this is the current record here. You've written my "post of the week".
回答 (2 件)
Matt Fig
2011 年 6 月 15 日
You are seeing the print out of length(a) then disp(i). I am thinking that this is a bug, or the parser is supposed to be so smart as to not let you error here. It certainly is unexpected!
I could see the parser recognizing a function call, but this works too:
a = 10;
for i = 7a
disp(i)
end
M-Lint underlines the a and says, "Use newline, semicolon or comma before this statement." So it is just a smart parser!
This works for more that FOR loops!
a = 10;
if 2a,end
and also,
while a==10a,end % ctrl+c ahead!
0 件のコメント
Jan
2011 年 6 月 15 日
Even in the command window of the ancient Matlab 6.5:
a = 10:12;
for i=2length(a), disp(8); end
>> ans =
3
8
>> disp(i)
i = 2
Strange. The parser seems to be lazy:
for i=2sin(1:4), end
>> ans = 0.8415 0.9093 0.1411 -0.7568
1 件のコメント
Paulo Silva
2011 年 6 月 15 日
Never seen such weird behavior, very interesting!
この質問は閉じられています。
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!