Change While condition based on if statement

6 ビュー (過去 30 日間)
ben
ben 2018 年 2 月 9 日
回答済み: Geoff Hayes 2018 年 2 月 9 日
Hi,
I would like make the following:
if PN == 1 || APN == 1
Condition 1 (Time<10)
else
Condition 2 (Vc>0)
end
While (Condition1/Condition2)
end
Is there a way to choose the condition for the while loop depends on the value I receive from the if statement?
Thank you.
  1 件のコメント
Adam
Adam 2018 年 2 月 9 日
It is possible to set this up yes, but your conditions are not in Matlab syntax so it is rather hard to see what exactly you are wanting to happen.

サインインしてコメントする。

回答 (2 件)

Rik
Rik 2018 年 2 月 9 日
This is not elegant, but it achieves your goal:
while ((PN == 1 || APN == 1) && Time<10) || ...
(~(PN == 1 || APN == 1) && Vc>0)
%code
end

Geoff Hayes
Geoff Hayes 2018 年 2 月 9 日
ben - I suppose you could use a function to evaluate your condition, and you would set the function based on the if/else from above. For example,
myConditionFunc = [];
if PN == 1 || APN == 2
myConditionFunc = @(x,y)x < 10;
else
myConditionFunc = @(x,y)y < 0;
end
So myConditionFunc takes two inputs which will be your Time and Vc. The while loop condition then looks like
while myConditionFunc(Time,Vc)
% something
end
Presumably, within your while loop, the Time and Vc are updated based on some other criterion.

カテゴリ

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