Hi, i want to make a loop return to key in input if the value does not met the condition, my code is like this:
prompt = 'What is your no. of car? ';
no_car= input(prompt);
prompt = 'Please key in your desireable SOC: ';
EV_Matrix(no_car,9) = input(prompt);
if EV_Matrix(no_car,9) < 20 | EV_Matrix(no_car,9) > 85
"ERROR:RETURN TO LINE PLEASE KEY IN YOUR DESIRABLE SOC"
end
How do i do this?

回答 (1 件)

David Hill
David Hill 2021 年 3 月 4 日

0 投票

prompt = 'Please key in your desireable SOC: ';
while 1
EV_Matrix(no_car,9) = input(prompt);
if EV_Matrix(no_car,9) >= 20 | EV_Matrix(no_car,9) <= 85
break;
end
end

1 件のコメント

Steven Lord
Steven Lord 2021 年 3 月 4 日
Rather than while 1 why not put the condition to break the loop in the while itself?
no_car = some_invalid_value;
while ~isValidValue(no_car)
no_car = askForValue;
end
with some_invalid_value, isValidValue, and askForValue being data or code that do what their names imply.

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

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

リリース

R2020a

タグ

コメント済み:

2021 年 3 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by