MATLAB Answers

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

3 views (last 30 days)
Joel Okanta
Joel Okanta on 19 Oct 2020
Edited: Adam Danz on 23 Oct 2020
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 Comment

Stephen Cobeldick
Stephen Cobeldick on 19 Oct 2020
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.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 19 Oct 2020
Edited: Adam Danz on 23 Oct 2020
"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.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!