How to error check a string for numbers?

7 次查看(过去 30 天)
PJ
PJ 2018-10-15
评论: PJ 2018-10-15
So I have an input
name = input('Please enter your name:\n', 's');
How can I error check so that a user cannot enter any numbers for it?
  2 个评论
madhan ravi
madhan ravi 2018-10-15
What should be the error check ,an example?
PJ
PJ 2018-10-15
If I enter 7 as an input, it should ask the user to enter a valid name

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2018-10-15
name = input('Please enter your name:\n', 's');
if isnan(str2double(name))
fprintf('Input is a string\n')
end
  1 个评论
PJ
PJ 2018-10-15
This gives me an error now if I enter a name but it gives me a go if I enter a number. I need it to do the opposite.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by