MATLAB: “if “function

if

how to write "IF" function to only accept numbers NO letters
need to ask input number (if user type any letters need to ask again to give number)
i know basic IF function but i need to know how to verified the input as number or letter ?
thank you Vijay

Best Answer

You can use the isstrprop function. If answer is the input you got from your user (using the input command),
all(isstrprop(answer, 'digit'))
Here's an example:
while true
answer = input('Enter a number: ', 's');
if all(isstrprop(answer, 'digit'))
answer = str2double(answer);
break;
else
disp('Only numbers supported');
end
end
Here's another way. This way is slightly better because it allows decimal points:
while true
answer = input('Enter a number: ', 's');
answer = str2double(answer);
if ~isnan(answer)
break;
else
disp('Only numbers supported');
end
end
Related Question