How to stop the loop once store the 1st value of a variable?

4 ビュー (過去 30 日間)
Sherif Shokry
Sherif Shokry 2018 年 2 月 17 日
編集済み: Sherif Shokry 2018 年 2 月 17 日
Dear all, In my code, I need to break the code after recording the 1st value once the condition satisfied. However, when I executed the following code, the "Swt_Tm" variable keep updating for each iteration while the condition is satisfied.
if strcmp (sg_12.get('AttValue','SigState'), 'GREEN')
Swt_Tm= sim.get('AttValue', 'SimSec');
end

採用された回答

Birdman
Birdman 2018 年 2 月 17 日
編集済み: Birdman 2018 年 2 月 17 日
Use break command inside the condition(note that break command works inside for and while loop):
for ...
%%%code
if strcmp (sg_12.get('AttValue','SigState'), 'GREEN')
Swt_Tm= sim.get('AttValue', 'SimSec');
break;
end
%%%code
end
  3 件のコメント
Birdman
Birdman 2018 年 2 月 17 日
while true
if strcmp (sg_12.get('AttValue','SigState'), 'GREEN')
Swt_Tm= sim.get('AttValue', 'SimSec');
break;
end
end
This will break the loop once the if condition is entered.
Sherif Shokry
Sherif Shokry 2018 年 2 月 17 日
When I tried this modification, of course the loop was broken. I believe it's my fault that I did not explain it well. My idea that I need to record the 1st value once the condition is satisfied for each iteration by keeping the loop going on. For example, I need to record the time point (say 10:30:12) when a green signal starts. However, my code keep updating this value while the condition is satisfied (the green time still running) Thus, I need to keep just the first recorded time for this cycle and for the next phase, it should be also record the first recorded time for the next cycle (say 10:32:03) and so on.

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

その他の回答 (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