Info

此问题已关闭。 请重新打开它进行编辑或回答。

What am I not seeing here

1 次查看(过去 30 天)
Gurudatha Pai
Gurudatha Pai 2011-6-15
关闭: MATLAB Answer Bot 2021-8-20
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
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
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!

Jan
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
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!

Translated by