# get user input in the form of an integer

251 vues (au cours des 30 derniers jours)
Robert le 5 Avr 2020
I want to get user input in the form of an integer
I cant beleive i have to go to the forum to find something so simple with matlab
I want to ask a user for 2 numbers in one input
Why is this so HARD!!!!
For example
user_input[min,max] = input("Please enter min and max in the form of [min,max]");
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Abdulrhman Atef le 21 Avr 2021
it is complicated but you can use this
test = 0;
while test==0
XX = input('please enter 1 or 2 : ');
if XX == 1
test = 1;
elseif XX == 2
test =1;
else
disp(' please enter either 1 or 2')
end
end

Connectez-vous pour commenter.

### Réponses (2)

David Hill le 5 Avr 2020
Modifié(e) : David Hill le 5 Avr 2020
The user inputs two integers and they are stored in the user_input array.
user_input = input('Please enter min and max in the form of [min,max]');
The user inputs are access via the array (user_input(1) or user_input(2)).
##### 5 commentairesAfficher 3 commentaires plus anciensMasquer 3 commentaires plus anciens
David Hill le 5 Avr 2020
Modifié(e) : David Hill le 5 Avr 2020
I am only trying to help. You can use the excellent MATLAB help resources.
help input
If you want to restrict the input you can establish a while loop:
user_input=0;
while length(user_input)~=2 || ~isequal(user_input,floor(user_input))%this ensures only two inputs and only integers
user_input = input('Please enter min and max in the form of [min,max]');
end
David Hill le 5 Avr 2020
You need to enter:
[5,6]
Just like your statement says....in the form of [min,max]!

Connectez-vous pour commenter.

Walter Roberson le 21 Avr 2021
You can use inputdlg() to request multiple boxes, and then str2double() the resulting cell array.
You can use input() and assume that the user will know to enclose the numbers in literal [ and ] characters.
You can use multiple input() prompts.
You can use input() with the 's' option to get a character vector, and post-process the character vector. For example
s = input('please enter min and max in the form [min, max]', 's');
n = str2double( regexp( regexprep(s, {'[[\]]', '[;,\s]+'}, {'', ' '}), '\s', 'split') ) );
if numel(n) ~= 2; error('blah'); end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur MATLAB Compiler dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by