Effacer les filtres
Effacer les filtres

Keeping the value of a constant once it reaches that value using IF else statement??

1 vue (au cours des 30 derniers jours)
Hello,
I'm trying to generate the following function:
function K = Table(Is,Imax,Phiref,Phi)
K=0;
Sa=0;
Sb=0;
Sc=0;
if Phi<0.98*Phiref
if Is<Imax+0.5
Sa=0;
Sb=1;
Sc=1;
else
Sa=0;
Sb=0;
Sc=0;
end
else
K=1;
end
Once that K=1, it must keep this value (1) during the rest of execution no matter the conditions in the function.
Thanks

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Mai 2016
function K = Table(Is,Imax,Phiref,Phi)
persistent stick
if isempty(stick); stick = 0; end
Sa=0;
Sb=0;
Sc=0;
if Phi<0.98*Phiref
if Is<Imax+0.5
Sa=0;
Sb=1;
Sc=1;
else
Sa=0;
Sb=0;
Sc=0;
end
else
stick = 1;
end
K = stick;
The only way to un-stick is to "clear Table", as you said it had to keep the value "no matter the conditions in the function", which tells us that no matter what inputs are given (such as if you wanted to start another loop) that Table needs to keep returning 1 if it has ever returned 1 at any point during the current MATLAB session.

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