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

20 visualizzazioni (ultimi 30 giorni)
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

Risposte (3)

Sean de Wolski
Sean de Wolski il 11 Mag 2011
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 il 11 Mag 2011
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 Commenti
Md. Abdullah AL Mamun
Md. Abdullah AL Mamun il 15 Mag 2018
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 il 15 Mag 2018
Modificato: Jan il 15 Mag 2018
@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

Accedi per commentare.


Matt Fig
Matt Fig il 11 Mag 2011
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)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by