Hi, I'm having trouble getting my script to prompt the user to re-enter the input if it isn't a number. I've seen a few similar questions but none seem to work for me.

1 vue (au cours des 30 derniers jours)
i = input('Please enter which you are converting from, ''imperial'' or ''metric'' : ', 's');
i = lower(i);
switch i
case 'imperial'
gallons = input('Please enter measurement in Gallons: ');
litres = (gallons*3.78544);
str = ['The value is equal to ' num2str(litres) ' Litres'];
disp(str)
case 'metric'
litres = input('please enter measurement in Litres: ');
gallons = (litres/3.78544);
str = ['The value is equal to ' num2str(gallons) ' Gallons'];
disp(str)
otherwise
str = ['That input is invalid.'];
disp(str)
VolConv;
end
%it works fine when I call for the VolConv.m file to run again, but in the case the user enters letters when asked for the measurement, I get errors. It does prompt them again until they enter a number, but I want to eliminate the red error message for one of my own and then prompt again. Thanks in advance!

Réponses (2)

Stephen23
Stephen23 le 6 Avr 2017
Modifié(e) : Stephen23 le 7 Avr 2017
Always use the 's' option with input, even when you want a numeric value, e.g.:
gallons = str2double(input('Please enter measurement in Gallons: ','s'));
This will prevent bad things from happening if the user inputs valid code (either accidentally or maliciously), and will output NaN for any invalid inputs (i.e. anything that is not a number). You can test for the NaN using isnan.
  3 commentaires
Stephen23
Stephen23 le 7 Avr 2017
@James Price: my mistake, the function you should use is str2double.
James Price
James Price le 21 Avr 2017
Ahh, Cool I'll try that one out. Apparently it's something that we don't need to address at this stage of our course, but I'll definitely play around with it once I've got the assigment finished Thanks!

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 6 Avr 2017
See this snippet. Put it inside a loop that calls it until your users enter the correct information:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
  2 commentaires
James Price
James Price le 21 Avr 2017
Thanks for your reply! We're not up to GUIs for this stage of our assignment, but the next stage does, so could do the job then. Thanks again!
Image Analyst
Image Analyst le 21 Avr 2017
You can use input() instead of inputdlg() if you're not allowed to use dialog boxes yet. input() is a more primitive command window-based function.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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