How do I evaluate string user input with switch statement?

13 ビュー (過去 30 日間)
Rob Reid
Rob Reid 2021 年 5 月 3 日
コメント済み: Rob Reid 2021 年 5 月 3 日
I'm looking to ask the user to input a simple yes/no response, and then evaluate the response using a switch statement. This is essentially my code:
userInput = input('This is a prompt [y/n]');
input = lower(input);
switch userInput
case 'y'
%statements
case 'n'
%statements
end
However, when I type in y or n and hit enter, the code doesn't drop in to the appropriate cases (I set some breakpoints to make sure). I'm guessing there's a mismatch between the data type the switch statement is looking for and the data type I'm actually inputting to the console, but I'm not sure how to resolve this.

採用された回答

Fangjun Jiang
Fangjun Jiang 2021 年 5 月 3 日
input('This is a prompt [y/n]','s') to input a string. Otherwise, it is for numerical.
  1 件のコメント
Rob Reid
Rob Reid 2021 年 5 月 3 日
Thanks, that works great. Much appreciated!

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by