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.
Best Answer