Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

What am I not seeing here

1 visualización (últimos 30 días)
Gurudatha Pai
Gurudatha Pai el 15 de Jun. de 2011
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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 comentario
Jan
Jan el 15 de Jun. de 2011
5 votes in 22 minutes - this is the current record here. You've written my "post of the week".

Respuestas (2)

Matt Fig
Matt Fig el 15 de Jun. de 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 el 15 de Jun. de 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 comentario
Paulo Silva
Paulo Silva el 15 de Jun. de 2011
Never seen such weird behavior, very interesting!

La pregunta está cerrada.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by