How can I suppress or change the error message 'Undefined function or variable'

6 views (last 30 days)
I am using the user request ‘input’ function to gather some data which is all supposed to be numeric. Obviously if the user enters a letter Matlab throws up the above error due to a data type clash:
Please enter a number: t
Error using input
Undefined function or variable 't'.
Error in finaltest (line 5)
number= input('Please enter a number:
');
Please enter a number:
I would like to suppress this error or, even better, change it to one of my own.
I have tried to find the error identifier, but to no avail:
EDU>> lasterror
ans =
message: [1x135 char]
identifier: [1x24 char]
stack: [1x1 struct]
Is it even possible to change this error?
Thanks in advance
Steve

Accepted Answer

Vivek Selvam
Vivek Selvam on 23 Oct 2013
Edited: Vivek Selvam on 23 Oct 2013
You can try this:
number = '';
% str2double returns NaN if input is not a real or complex scalar value
while isnan(str2double(number))
number= input('Please enter a number: ','s'); % get input as string
end
disp(['You entered ' number]);
number = str2double(number);

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!