how to increment variable while logical true?

14 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Image Analyst
Image Analyst il 21 Gen 2015
Modificato: Image Analyst il 21 Gen 2015
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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by