Validate number in function from user input
Show older comments
Hi there,
I am having some trouble debugging my function. When I run, it seems to bug on the while loop but I cannot figure out why after several attempts.
I would like to have the user input a positive number or empty value, []. If not, repeat the input prompt. In addition, I must use str2double to convert text strings.
MY FUNK:
function valid_number = ask_for_number()
txt_input = input ('Enter a number:\n\n','s');
valid_number = str2double(txt_input);
if (nargin > 1);
error('Enter a single number');
txt_input = input('Enter a number:\n\n', 's');
end
while valid_number <=0;
fprintf('\n The number entered must be positive or empty \n\n');
txt_input = input ('Enter a number:\n\n');
end
if ~isnumeric(valid_number);
txt_input = input ('Enter a number:\n\n','s');
end
if (isempty(txt_input) & isnan(txt_input));
valid_number = [];
end
valid_number = str2double(txt_input);
end
Merci beaucoup for your help.
Accepted Answer
More Answers (2)
dpb
on 5 Feb 2015
nargin checks for number of arguments into the called function; your function doesn't have room for any so that whole clause is useless. If your intent is to test whether there were two or more discernible values returned from parsing the input string by str2double, use
if numel(valid_number)>1
But, for the user to do this requires they surround the input string in curlies and create a valid cell array of strings so is highly unlikely altho it doesn't hurt to check.
More to your actual question pose, you've got the while loop enclosing only an input statement and then the function continues on down to the end before it tries to evaluate it. At that point there is no more checking however, whatever is the result of the last str2double call is what your function will return regardless of all the other testing.
You need the while...end loop to enclose essentially the entire function body and use a logic variable to control it rather than the result...
isOK=false;
while ~isOK
txt_input=input(...
valid=str2double(txt);
if numel(valid)==1 & valid>0
isOK=true; % ok, we can quit
else
...various errors here...
end
...
end
4 Comments
"...requires they surround the input string in curlies and create a valid cell array of strings". Given that the input to str2double is defined as the string output from input, this can never happen in this function. How will the user ever be able to define a cell array of strings from the prompt of input('...','s') ?
str2double can only parse one value, as the documentation says: If str does not represent a valid scalar value, str2double returns NaN.
>> str2double('1 2') % is possible...
ans = NaN
>> str2double({'1','2'}) % <- can never happen in this function!
ans =
1 2
Which means this statement
numel(valid_number)>1
will always be false. I guess it could be considered "defensive programming" and might be relevant in mature distributed code, but considering the basic functionality is more of an issue here, I suspect bringing this up just makes the whole thing more difficult to follow and comprehend.
Stephen23
on 5 Feb 2015
Something like this:
isOK = false;
while ~isOK
txt_input = input('prompt text: ','s');
is_valid = str2double(txt_input);
if is_valid>0
isOK=true; % ok, we can quit
else
... errors
end
end
Does the same job.
dpb
on 5 Feb 2015
...How will the user ever be able to define a cell array of strings from the prompt of input('...','s') ?...
As precisely written, can't, agreed. It was a description of what inferred OP had in mind when trying to write the testing, not intended as literal, necessarily.
Something as simple as removing the 's' parameter from the input or more diabolical (but surely not unheard of and particularly often with newbies) is an eval. will work quite nicely, however....
>> s=input('enter: ')
enter: {'2','3'}
s =
'2' '3'
>> str2double(s)
ans =
2 3
>> >> s=input('enter: ','s')
enter: {'2','3'}
s =
{'2','3'}
>> eval(s)
ans =
'2' '3'
>> str2double(ans)
ans =
2 3
>>
True, removing the 's' could allow this situation to occur, and your suggestion of "defensive programming" to check for this works well. I guess for me getting them to understand the functionality is the first priority, which makes this check unnecessary. Although in this circumstance you are probably right: this small change could occur quite easily and without the OP being aware of its ramifications, which makes the small bit of extra code required worthwhile.
PR
on 5 Feb 2015
0 votes
1 Comment
It is daunting starting to learn something new, but it just takes time... good luck and enjoy the journey! And of course, come and ask questions if things don't make sense. Use the internet too, there are a million examples online. And of course do not forget the Documentation :)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!