Analysing the user's input to see if it's a number or not

5 次查看(过去 30 天)
I created a code with the intentions of prompting the user for their inputs, with these inputs being appropriate. This means that the inputs had to be integers/real numbers and that the start time had to be smaller than the stop time with the increment being appropriate between the limits. Unfortunately, I bumped into some issues such as:
  • Not knowing how to set a limit for the increment
  • Code not working after trying to force the user to input a number.
My code:
%User input for start and stop times
s = 1;
prompt = {'Enter Start time:','Enter increment:','Enter Stop time:'};
title = 'Input';
resume = 'Program execution resumed';
dims = [1, 35];
answer = inputdlg(prompt,title, dims)
start = str2num(answer{1})
increment = str2num(answer{2})
stop = str2num(answer{3})
integer_check1 = isnumeric(start)
integer_check2 = isnumeric(increment)
integer_check3 = isnumeric(stop)
while start > stop
error = msgbox('ERROR: Start cannot be larger than Stop time. Try again')
uiwait(error)
disp(resume)
answer = inputdlg(prompt,title, dims)
start = str2num(answer{1})
increment = str2num(answer{2})
stop = str2num(answer{3})
if integer_check1 == 1 | integer_check2 == 1 | integer_check3 == 1
error2 = msgbox('ERROR: Integer Only')
uiwait(error2)
disp(resume)
answer = inputdlg(prompt,title, dims)
start = str2num(answer{1})
increment = str2num(answer{2})
stop = str2num(answer{3})
disp('Working')
end
disp('This is legal')
end
I did my research and used the functions isreal, isnumeric and isinteger. They did not work and instead just stopped the program after i spammed random letters in the input box, no errors at all.
  1 个评论
Stephen23
Stephen23 2020-10-19
Your code does not change the values of integer_check1 or integer_check2 or integer_check3 inside the loop, so any values provided inside the loop are not checked by your algorithm.
Rather than str2num (which hides an evil eval inside, which can have dangerous/unexpected side effects) you would be much better off using str2double or sscanf or a regular expression.

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2020-10-19
编辑:Adam Danz 2020-10-23
"inputs had to be integers/real numbers and that the start time had to be smaller than the stop time with the increment being appropriate between the limits."
Matlab offers several input validation methods. I like validateattributes because its been around for a while and will work on older releases, it's versatile, and it has built-in error message depending on which criteria were violated.
validateattributes(start,{'numeric'},{'integer','real','<',stop},'MyFunction','start')
validateattributes(stop,{'numeric'},{'integer','real','>',start},'MyFunction','stop')
validateattributes(increment ,{'numeric'},{'integer','real','<',stop-start},'MyFunction','increment')
Example of error message:
start = 50.5; % This is not an integer, will cause an error
validateattributes(start,{'numeric'},{'integer','real','<',stop},'MyFunction','start')
Error using MyFunction
Expected start to be integer-valued.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by