필터 지우기
필터 지우기

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