if, or or or or or
181 ビュー (過去 30 日間)
古いコメントを表示
I'm just wondering if Matlab can string together "or" statements? The current code for a simple menu is using
userinput = input("Select 1,2,3,4 and press enter: ")
if X == 1
disp(stuff)
if X == 2
disp(stuff)
% etc. this is just an example
end
Seems like you should be able to use if X=(1||2||3||4) to avoid the multiple lines of code. Then:
if X = (1||2||3||4)
Y= sprintf ("You selected: ", userinput)
disp(Y)
else
disp("That was not a choice")
end
We're on to using C++ now so I'm thinking along the lines of
cout <<"You seleceted"<< variable<< endl;
This is probably better done as a switch case but I was thinking about booleans when I started to try this.
0 件のコメント
採用された回答
Cris LaPierre
2018 年 12 月 12 日
編集済み: Cris LaPierre
2018 年 12 月 12 日
It can, but you have to be a little more specific about it
if X == 1 || X == 2 || X == 3 || X == 4
9 件のコメント
その他の回答 (1 件)
Image Analyst
2018 年 12 月 12 日
Of course MATLAB will let you use input() multiple times.
You might want to consider menu():
buttonNumber = menu('Make a selection', '1', '2', '3', '4');
if buttonNumber == 1
% Code to execute if they clicked 1.
elseif buttonNumber == 2
% Code to execute if they clicked 2.
elseif buttonNumber == 3
% Code to execute if they clicked 3.
elseif buttonNumber == 4
% Code to execute if they clicked 4.
end
4 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!