0
$\begingroup$

This is a peice of code written in Matlab:

while flag==1
    flag = 0;
    disp('end of loop');
end

Surprisingly, this does not display the string. If I remove the disp statement a figure1 window appears. Can anyone explain why?

$\endgroup$
  • 3
    $\begingroup$ Since this question is more about programming than computational science, I would suggest moving it to stack overflow. $\endgroup$ – Dr_Sam Oct 28 '13 at 12:33
5
$\begingroup$

That is simply because flag is a defined command in matlab. Try typing flag just after having started Matlab and you will see a non-empty matrix appearing. If you type help flag you will see the meaning of that function.

Also, the function is selected by Matlab because you do not declare a variable called flag before your code. If you write flag=1 before your code, it works as expected.

Still, the best way is to change the name of your variable in order not to hide the Matlab function. So, use the following code:

myflag = 1;
while( myflag == 1)
    myflag = 0;
    disp('end of loop');
end
| cite | improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.