Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Vivek Selvam
Vivek Selvam il 23 Ott 2013
Modificato: Vivek Selvam il 23 Ott 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);

Più risposte (0)

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by