How to test to see if a user input is not a number?

48 views (last 30 days)
MATLAB
MATLAB on 28 Jul 2021
Commented: Rik on 28 Jul 2021
y=input('Input a number')
if isnumeric(y) ~= 1
disp('The entry is not a number')
end
I am asking the user to input a number. I would like to know how to test if a user has entered something, for example 'g'. At the moment when I input this, it says that 'g' is an unrecognised variable. I would like to know how to do this using the 'isnumeric' function.
  1 Comment
Rik
Rik on 28 Jul 2021
I would suggest the strategy KSSV posted, as that circumvents problems like this:
y='1.2';
isnumeric(y)
ans = logical
0

Sign in to comment.

Answers (1)

KSSV
KSSV on 28 Jul 2021
y = input('Input a number: ','s') ;
if isnan(str2double(y))
disp('The entry is not a number')
end

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!