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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2016-5-10
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by