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

3 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by