How to test to see if a user input is not a number?
34 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Rik
2021 年 7 月 28 日
I would suggest the strategy KSSV posted, as that circumvents problems like this:
y='1.2';
isnumeric(y)
回答 (1 件)
KSSV
2021 年 7 月 28 日
y = input('Input a number: ','s') ;
if isnan(str2double(y))
disp('The entry is not a number')
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!