if statement after use input

14 ビュー (過去 30 日間)
Richard
Richard 2012 年 2 月 28 日
Is there a possibility of performing an if statement following an user input. For exampl:
Variable = input('type 1,2, or 3 :');
Then stating that if the user typed 1...
I would like to use different values in my equations depending on the value inserted by the user, is this possible?

採用された回答

Wayne King
Wayne King 2012 年 2 月 28 日
Yes,
Variablein = input('type 1,2,or 3:\n');
if (Variablein == 3)
disp('You typed 3');
end
For a string:
Variablein = input('type 1,2,or 3:\n','s');
Variablein = str2num(Variablein);
if (Variablein == 3)
disp('You typed 3');
end
  2 件のコメント
Richard
Richard 2012 年 2 月 28 日
and what if the use input is a string not a number?
Wayne King
Wayne King 2012 年 2 月 28 日
I updated my example to show that.

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

その他の回答 (1 件)

Oleg Komarov
Oleg Komarov 2012 年 2 月 28 日
I don't understand probably, but:
Variable = input('type 1,2, or 3 :')
if Variable == 1
...
elseif Variable == 2
...
elseif Variable == 3
...
else
...
end
Or use a switch case statement.

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by