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

조회 수: 39 (최근 30일)
Simon T
Simon T 2020년 4월 23일
댓글: Daniel Prieto 2023년 2월 11일
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.

채택된 답변

Stephen23
Stephen23 2020년 4월 23일
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
Stephen23
Stephen23 2020년 4월 23일
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 2023년 2월 11일
yeah what is " ismember" and "isnan", very unfamiliar with Matlab and none of that is making sence

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2020년 4월 23일
  댓글 수: 1
Simon T
Simon T 2020년 4월 23일
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.

댓글을 달려면 로그인하십시오.


madhan ravi
madhan ravi 2020년 4월 23일
편집: madhan ravi 2020년 4월 23일
  댓글 수: 2
Simon T
Simon T 2020년 4월 23일
I'll give this a try - how do I check the "if numeric" part?
madhan ravi
madhan ravi 2020년 4월 23일
편집: madhan ravi 2020년 4월 23일
You already did isnumeric()

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by