if statement after use input

Asked by Richard on 28 Feb 2012
Latest activity Commented on by Wayne King on 28 Feb 2012

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?

0 Comments

Richard

Products

No products are associated with this question.

2 Answers

Answer by Wayne King on 28 Feb 2012
Accepted answer

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 Comments

Richard on 28 Feb 2012

and what if the use input is a string not a number?

Wayne King on 28 Feb 2012

I updated my example to show that.

Wayne King
Answer by Oleg Komarov on 28 Feb 2012

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.

0 Comments

Oleg Komarov

Contact us