Verifying user input
显示 更早的评论
Hi there,
How can you verify numerical input from a user. I have this little program here, and it works fine until the user enters a non numerical input. How do you get matlab to check if it is a number or character?
Thanks in advance
采纳的回答
更多回答(2 个)
Jan
2011-2-23
A simple test is letting SSCANF count the number of accepted characters:
s = '0.12345'
sFull = deblank(s);
[num, count, errmsg, nextIndex] = sscanf(sFull, '%g', 1);
if nextIndex == length(sFull + 1) && count == 1
fprintf('This is a scalar number: %s\n', sFull)
else
fprintf(2, 'This is not a scalar number: %s\n', sFull)
end
Robert Cumming
2011-2-23
0 个投票
Check what the user inputs using something like:
isnumeric
see help isnumeric to get a list of similar checks you can perform
3 个评论
Jan
2011-2-23
I'm sure, Robert means the contents of a string input, while ISNUMERIC checks the type of a variable.
Robert Cumming
2011-2-23
well spotted! ;)
Indeed it depends on what he is doing with the user input, if he is converting the string input to a number he could check if that was valid using isempty
OLUBUKOLA ogunsola
2016-6-14
this post is old, i hope someone reads this . what if i want to check if input is numeric and the 26 alphabets, nothing else is allowed . how can i do this
类别
在 帮助中心 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!