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

26 ビュー (過去 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 件のコメント
Abdullah AL Shohag
Abdullah AL Shohag 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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by