how to increment variable while logical true?

14 vues (au cours des 30 derniers jours)
akshay raj
akshay raj le 21 Jan 2015
Commenté : akshay raj le 21 Jan 2015
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!

Réponse acceptée

Image Analyst
Image Analyst le 21 Jan 2015
Modifié(e) : Image Analyst le 21 Jan 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
  1 commentaire
akshay raj
akshay raj le 21 Jan 2015
thats a silly mistake. thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by