필터 지우기
필터 지우기

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.

조회 수: 2 (최근 30일)
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!

답변 (2개)

Stephen23
Stephen23 2017년 4월 6일
편집: Stephen23 2017년 4월 7일
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
Stephen23
Stephen23 2017년 4월 7일
@James Price: my mistake, the function you should use is str2double.
James Price
James Price 2017년 4월 21일
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!

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


Image Analyst
Image Analyst 2017년 4월 6일
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
James Price
James Price 2017년 4월 21일
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 2017년 4월 21일
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.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by