Info

This question is closed. Reopen it to edit or answer.

What am I not seeing here

1 view (last 30 days)
Gurudatha Pai
Gurudatha Pai on 15 Jun 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
Jan
Jan on 15 Jun 2011
5 votes in 22 minutes - this is the current record here. You've written my "post of the week".

Answers (2)

Matt Fig
Matt Fig on 15 Jun 2011
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 on 15 Jun 2011
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 Comment
Paulo Silva
Paulo Silva on 15 Jun 2011
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!