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

41 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Abr. de 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 comentarios
Stephen23
Stephen23 el 23 de Abr. de 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 el 11 de Feb. de 2023
yeah what is " ismember" and "isnan", very unfamiliar with Matlab and none of that is making sence

Iniciar sesión para comentar.

Más respuestas (2)

KSSV
KSSV el 23 de Abr. de 2020
  1 comentario
Simon T
Simon T el 23 de Abr. de 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.

Iniciar sesión para comentar.


madhan ravi
madhan ravi el 23 de Abr. de 2020
Editada: madhan ravi el 23 de Abr. de 2020
  2 comentarios
Simon T
Simon T el 23 de Abr. de 2020
I'll give this a try - how do I check the "if numeric" part?
madhan ravi
madhan ravi el 23 de Abr. de 2020
Editada: madhan ravi el 23 de Abr. de 2020
You already did isnumeric()

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by