MATLAB: How to increment variable while logical true

while loop

Hi,
I am new to matlab, I want to increment a number when its true.
i tried doing this as a script
global variable;
variable = true;
while (variable)
a=1;
a=a+1;
dips(a);
end
shouldn't this display an incremented value
1
2
3
4
.
.
.
but all i get is
2
2
2
2
.
.
Am i doing something wrong? help!

Best Answer

You're setting a back equal to 1 inside the loop. Why? That should be outside the loop. I think you should look at this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
global variable;
variable = true;
a = 1;
while variable
a=a+1
if a > 20
variable = false; % Break out of loop.
end
end
Related Question