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

3 ビュー (過去 30 日間)
StevieNewRoad
StevieNewRoad 2013 年 10 月 23 日
編集済み: Vivek Selvam 2013 年 10 月 23 日
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

採用された回答

Vivek Selvam
Vivek Selvam 2013 年 10 月 23 日
編集済み: Vivek Selvam 2013 年 10 月 23 日
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 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by