MATLAB: Store a variable from prompt user input

promptuser input

Hi, I have this function which display a request in the prompt and asks user to input a number. The function then repeats until user inputs a valid number.
function num = inputNumber(prompt)
while true
num = str2double(input('Write your age :', 's'));
if ~isnan(num)
break;
end
end
I want then to store the input in the variable num but it is not happening. Why? How can I solve it? Thanks

Best Answer

I changed it so that there is a limit to how many attempts can be made, and also that the output is always defined:
function out = inputNumber(prompt)
out = NaN;
tot = 3; % how many attempts to make
while isnan(out) && tot>0
out = str2double(input('Write your age :', 's'));
tot = tot-1;
end
end
and tested:
>> num = inputNumber()
Write your age: 64
num = 64
Related Question