Use User Input as a Number to be Checked in If/Else Statement
33 views (last 30 days)
Show older comments
Hello,
I'm having issues figuring this out, even though it seems it should be relatively simple.
I have the user input a number, and that value is then used to determine which cell in a vector is used for a different command.
Based on what I gathered, it seems that User Input is always a string (I could be mistaken), and not considered a numeric value, rather a character.
I have something like this:
prompt = 'Which Day?'
take1 = str2double(input(prompt)) % Take the number the user inputs and use it as a numeric number
if take1==1 || take1==2 || take1>N
% Here I want to check if the Number the user inputs is a certain value
% It can't be 1 or 2 and had to be less than a certain value
disp('Pick Again')
% Then I'll have another prompt for them to choose a different value
elseif -1^(take1*2)==1
% Here I want to make sure the user inputs an integer (no decimals)
% If this is true, I'll use the inputted number
else
disp('Pick Again')
% Another prompt here too
end
I keep on getting error messages, which I think lies in how I'm using the inputted value.
I might end up doing another check to be sure the user does not input another value.
Any help is appreciated.
Also, on a side note, is it possible to have these if/else statement loop back to the initial prompt?
So rather than in each if or elseif section I create a new UI prompt, the script goes back up to the first 'Which day?'.
Thanks.
1 Comment
Stephen23
on 20 Dec 2022
Edited: Stephen23
on 20 Dec 2022
" it seems that User Input is always a string (I could be mistaken)"
You are mistaken: if you do not use the 's' option then its output depends much on what the user types. As the documentation explains, the user can also use INPUT() to enter expressions, which could lead to unexpected side effects.
This is why experienced MATLAB users strongly recommend that for robustness and security you should use the 's' option and STR2DOUBLE() if the expected data are scalar numeric:
take1 = str2double(input(prompt,'s'));
Writing better code is simple, when you know how.
Accepted Answer
Voss
on 20 Dec 2022
Edited: Voss
on 20 Dec 2022
"it seems that User Input is always a string"
The value returned from the input function would be a character vector if you used the 's' option. Without the 's' the returned value will be numeric if the user enters a number.
You are expecting a number, and you are not using 's', which is consistent; you just need to avoid the call to str2double. (str2double with a numeric argument returns NaN.)
"is it possible to have these if/else statement loop back to the initial prompt?"
Yes. Use a while loop.
prompt = 'Which Day?'
while true
take1 = input(prompt); % Take the number the user inputs and use it as a numeric number
if take1==1 || take1==2 || take1>N
% Here I want to check if the Number the user inputs is a certain value
% It can't be 1 or 2 and had to be less than a certain value
disp('Pick Again')
% Then I'll have another prompt for them to choose a different value
elseif rem(take1,1) == 0 % (-1)^(take1*2)==1
% Here I want to make sure the user inputs an integer (no decimals)
% If this is true, I'll use the inputted number
break % exit the while loop
else
disp('Pick Again')
% Another prompt here too
end
end
Note that this considers 0 and negative integers to be valid inputs, and it will throw an error for non-scalar inputs.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!