How do I ensure an input as a number and not a letter/set of letters?

41 views (last 30 days)
I've tried a lot of methods for checking if my input x is a number or letter.
I understand that normally variables are set to double data types. So when I put in a letter or a word, understandably MATLAB freaks out and I get a sea of red.
This part of my program selects from one of 14 functions. If you input 1 when prompted, you get the first function, if you input 2, you get the second, etc.
I need to check to make sure that the user is only inputting a number from 1 to 14, and doing this involves checking if the user has put in random letters or something that isn't a number (e.g. "eihsgdf").
This is what I have:
xIsNumber = 0;
while xIsNumber == 0
x = input("Write the number of the converter you want to open here: "); %User enters what number converter to open
if isnumeric(x) == 0 || x ~= 1 || x ~= 2 || x ~= 3 || x ~= 4 || x ~= 5 || x ~= 6 || x ~= 7 || x ~= 8 || x ~= 9 || x ~= 10 || x ~= 11 || x ~= 12 || x ~= 13 || x ~= 14 %Make sure the input is only from 1 to 14
disp("You have entered something wrong for your choice of converter. Please ensure you're typing ONLY a number from 1 to 14.") %User is notified that their input is wrong
xIsNumber = 0; %Loops back to the x = input() above
else
xIsNumber = 1; %Escape the loop since the correct input has been made
end
end
I know this can be simplified, but I'm just looking to see if the input 'x' is a number from 1 to 14.
I get this if i put in "gfd" for example:
Write the number of the converter you want to open here: gfd
Error using input
Unrecognized function or variable 'gfd'.
Error in script (line 35)
x = input("Write the number of the converter you want
to open here: "); %User enters what number converter to
open
I know that putting letters in freaks the program out as mentioned above, so how do I avoid this? I tried making 'x' a string and going from there but the problem flipped and it didn't detect when I DID put a number from 1 to 14.
I'm very confused on what to do.

Accepted Answer

Stephen23
Stephen23 on 23 Apr 2020
It is more robust to return a character vector from input:
p = 'Write the number of the converter you want to open here: ';
x = [];
while isempty(x)
tmp = str2double(input(p,'s'));
if ismember(tmp,1:14)
x = tmp;
else
disp('try again')
end
end
  3 Comments
Stephen23
Stephen23 on 23 Apr 2020
A compact version:
x = NaN;
while isnan(x)
x = str2double(input('Enter an integer from 1 to 14: ','s'));
x(~ismember(x,1:14)) = NaN;
end
Daniel Prieto
Daniel Prieto on 11 Feb 2023
yeah what is " ismember" and "isnan", very unfamiliar with Matlab and none of that is making sence

Sign in to comment.

More Answers (2)

KSSV
KSSV on 23 Apr 2020
  1 Comment
Simon T
Simon T on 23 Apr 2020
I tried that, and the problem is in the x = input(...) line.
The program can't pass that line unless something is done before or during that line.

Sign in to comment.


madhan ravi
madhan ravi on 23 Apr 2020
Edited: madhan ravi on 23 Apr 2020

Categories

Find more on Tables 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!