Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!