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
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
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')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!