I want to throw the error that comes with entering a non number in a numeric only input

21 次查看(过去 30 天)
n = input('Please enter a number:\n');
I get this error and I want to throw it and write another error:
Error using input
Unrecognized function or variable 'k'.
Error in eer (line 1)
n = input('Please enter your name:\n');

采纳的回答

Wan Ji
Wan Ji 2021-8-30
n = str2num(input('Please enter a number:\n','s'));
if(isempty(n))
error('You are entering a non number in a numeric only input')
end
  10 个评论
Wan Ji
Wan Ji 2021-8-30
n = input('Please enter a number:\n','s');
if(isempty(n))
error('You are entering nothing')
elseif(isempty(str2num(n)))
error('You are entering a non number in a numeric only input')
end
n = str2num(n);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by