How can I suppress or change the error message 'Undefined function or variable'
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
StevieNewRoad
il 23 Ott 2013
Modificato: Vivek Selvam
il 23 Ott 2013
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
0 Commenti
Risposta accettata
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);
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!