필터 지우기
필터 지우기

Let users enter integer, warn them if they enter anything else

조회 수: 9 (최근 30일)
J
J 2011년 5월 11일
편집: Jan 2018년 5월 15일
I want users to enter an integer. If they enter anything else I want to display a warning message. This is my best attempt so far
input_hf = input('Enter an integer: ');
if ~isnumeric(input_hf) || ~fix(input_hf) == input_hf
disp('Please enter an integer')
end
But it doesn't work. When users enter a string the application crashes. Here are the results from a failed test run:
Enter an integer: a
??? Error using ==> input
Undefined function or variable 'a'.
Error in ==> skapaPlot at 4
input_hf = input('Enter an integer: ');
How can I change my code to make it work as intended?
Thanks

답변 (3개)

Sean de Wolski
Sean de Wolski 2011년 5월 11일
input_hf = str2double(input('Enter an integer: ','s'));
if isnan(input_hf) || fix(input_hf) ~= input_hf
disp('Please enter an integer')
end

Andy
Andy 2011년 5월 11일
From the documentation of input: "The response to the input prompt can be any MATLAB expression, which is evaluated using the variables in the current workspace." When you put in a, MATLAB tries to evaluate it. If you just want to return the string the user inputs, then you need to use the second argument 's' of input (again, see the documentation). Only then, as Sean de pointed out, should you try to convert to a double. At this point, if the input was not numeric, str2double returns NaN, so you can alter your conditional check (and I'll throw it in a while loop as well, so that it asks more persistently) to get:
input_hf = str2double(input('Enter an integer: ', 's'));
while isnan(input_hf) || fix(input_hf) ~= input_hf
input_hf = str2double(input('Please enter and INTEGER: ', 's'));
end
  댓글 수: 2
Md. Abdullah AL Mamun
Md. Abdullah AL Mamun 2018년 5월 15일
But this don't take more than two string. If I input 2 string, first time it says 'Please enter an integer'.
But next time it executes the file without any value 'NaN'. How to make a return loop?
Jan
Jan 2018년 5월 15일
편집: Jan 2018년 5월 15일
@Abdullah AL Shohag: Please use flags only to inform admins and editors about contributions, which might conflict with the terms of use, e.g. by rudeness or spam.
You cannot input 2 strings by one input command. You can enter one string containing 2 numbers.
To accept more numbers:
num = NaN;
while any(isnan(num(:))) || ~all(fix(num(:)) ~= num(:)) ...
|| (numel(num) ~= 2)
s = input('Please enter two INTEGERS: ', 's');
s = strrep(',', ' ');
num = sscanf(s, '%g %g');
end

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


Matt Fig
Matt Fig 2011년 5월 11일
a is not a string. In MATLAB a string has a single quote. This is how you enter a string:
Enter an integer: 'a'
When you just type a, MATLAB looks for a variable named a, as in:
a = 5; % Now a is defined!
Enter an integer: a
If the user types just a with no quotes, that is a user mistake and has nothing to do with your code. It is like doing this:
tan(b) % Without defining b first, this will error....
This understanding lets the user enter a pre-defined integer as above (just like with the call to TAN). Also, the user can do like:
Enter an integer: floor(pi)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by