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.

 Risposta accettata

Cris LaPierre
Cris LaPierre il 12 Dic 2018
Modificato: Cris LaPierre il 12 Dic 2018

0 voti

It can, but you have to be a little more specific about it
if X == 1 || X == 2 || X == 3 || X == 4

9 Commenti

Cris LaPierre
Cris LaPierre il 12 Dic 2018
Modificato: Cris LaPierre il 12 Dic 2018
You could also use 'ismember' instead
if ismember(X,[1,2,3,4])
furcifer
furcifer il 12 Dic 2018
Modificato: furcifer il 12 Dic 2018
Wait, I wasn't specific enough. It doesn't seem to work for what I have:
eta: and I can't seem to use this new text editor. I can't input text on the other side of this box for some reason
if (answer) == (1|2|3|4)
James Tursa
James Tursa il 12 Dic 2018
Show us your complete code and we can help correct your syntax.
furcifer
furcifer il 12 Dic 2018
Modificato: furcifer il 12 Dic 2018
This is what I was thinking:
clear
clc
X=0;
answer = input("Select 1,2,3,4 and press enter");
if (answer) == (1|2|3|4)
%eta I think X=answer and then
if X == 1 || X == 2 || X == 3 || X == 4
%should work
X = sprintf("You Selected",answer);
disp(X)
else
disp("That was not a choice!");
end
Walter Roberson
Walter Roberson il 12 Dic 2018
I can't input text on the other side of this box for some reason
Click or otherwise position after the last character of your last line of code input. Click on the "Text" icon (far left one).
On Mac with Firefox, the key sequence would be to position somewhere on the last line of code, then press control-E to move to end of line, then press alt-enter to switch to text mode.
if ismember(answer, [1, 2, 3, 4])
Your approach of (1|2|3|4) will not work in MATLAB. That will be considered to be equivalent to
if (answer) == ((1 ~= 0) | (2 ~= 0) | (3 ~= 0) | (4 ~= 0))
The ((1 ~= 0) | (2 ~= 0) | (3 ~= 0) | (4 ~= 0)) part would evaluate to (true | true | true | true) which would be true. Then the test would have become
if (answer) == true
There is no hope that MATLAB will ever support a syntax like
(answer) == (1|2|3|4)
furcifer
furcifer il 13 Dic 2018
I ran that, it works but Matlab doesn't seem to like sprintf ("string", variable)
X = sprintf("You Selected %d",answer);
furcifer
furcifer il 13 Dic 2018
Thanks. I think I got it now. I forgot to save the code and was getting a weird error.
Thanks for all the help.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 12 Dic 2018

0 voti

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 Commenti

furcifer
furcifer il 12 Dic 2018
I was looking to get rid of the "elseif" statements. If it was any 3 digit number it would be a lot of elseif's!
It's a bit of a weird question. I guess I was thinking about using arrays to generate all the possible selections and then combining them with a string. If that makes sense.
You can use a switch statement but a switch statement will have one more line of code than an if/else.
Not sure how this full factorial combination generation followed by putting into a string method you're thinking of would work. I can't imagine it being simpler than a simple if/else.
How many are you going to have anyway? Hundreds? My programs typically have well over a thousand lines of code, so what's the big deal on having 3 or 4 extra lines if it's clearer and more intuitive, meaning easier to maintain? Do you really want some tricky, cryptic code just to save a line or two? I'd think not.
Not sure why you accepted that answer
if X == 1 || X == 2 || X == 3 || X == 4
when you can much more simply do
if X <= 4
assuming your user did what you told them to. Anyway, you're still going to have to check for the numbers individually if you want to execute different code depending on the number. So I don't understand why the accepted Answer is ideal. But whatever - it's your program and you can make it as simple or cryptic as you want.
furcifer
furcifer il 13 Dic 2018
I'm studying Matlab and C++ at the same time for an exam. The questions are going to specifically state to use "while", "do while", "if" etc. Looking at this type of question it's possible it could be asked to use boolean operators.
X <= 4
isn't quite as "idiot proof". -4 isn't a choice but the program returns a value. This isn't so much about saving code as trying to make sure how I think gets translated into what's being asked so if my code isn't excatly the same it functions the same way.
Walter Roberson
Walter Roberson il 13 Dic 2018
x >= 1 && x <= 4

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by