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

 采纳的回答

If you're using a Command Window interface, then you could employ this function, which I wrote a while back. I use it to ensure the user gives a numerical input. It also insists on a scalar value. You can easily modify it to ensure that the number is real, and/or an integer, as well, by modifying the subfunction checknum.
function i = getnum(prompt, default)
% GETNUM Read a numerical value from the keyboard
%
% I = GETNUM(PROMPT) prints the string PROMPT and waits for the user
% to type a number followed by return. Checks that the input does
% actually represent a scalar and prompts again if necessary.
% Returns the number.
%
% I = GETNUM(PROMPT, DEFAULT) does the same except that the number
% DEFAULT is printed after the prompt, and is returned if the user
% just presses return. Setting DEFAULT to [] is the same as omitting
% it.
% David Young, March 2002
if nargin < 2
default = [];
end
if isempty(default)
str = prompt;
else
if ~checknum(default)
error('Expecting numerical default argument');
end;
str = [prompt, ' [', num2str(default), '] '];
end;
i = [];
while isempty(i)
i = input(str, 's');
if isempty(i) && ~isempty(default)
i = default;
else
i = str2num(i); %#ok<ST2NM>
if ~checknum(i)
disp('A single number expected');
i = [];
end
end
end
end
function i = checknum(x)
% Checks whether its argument is a single number
i = isnumeric(x) && isscalar(x);
end

2 个评论

Thank you, this shows that what I am after is actually much more complex than what I am being asked to produce. My program is only 10 lines long, and is for counting snooker balls on a table, I managed to get it so that you had to enter from 1 - 5, but getting numerical or character seems more complex.
You need to decide how complex your check needs to be.
David's example is very thorough – e.g. he has default value setting (do you need that?). Your check can be anything from a couple of lines to this complete check.

请先登录,再进行评论。

更多回答(2 个)

Jan
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
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 个评论

I'm sure, Robert means the contents of a string input, while ISNUMERIC checks the type of a variable.
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
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!

Translated by