Im attempting to debug erroneous input to retreive a valid input from a user.
I want the input to be only a numeric value and am using isstrprop to test if the input is a digit,
however isstrprop(input, 'digit') returns false if the value has a decimal place. (as a period is not a digit)
Can I use isstrprop to search for digits and decimal places, or is there a better way of acheiving the same result?
Here is my code:
clear, clc;NumInput = ('');while isempty(NumInput) NumInput = input('Hello!\nWelcome to my unit converter!\nPlease enter a numerical value to convert:\n\n', 's'); NumTest = isstrprop(NumInput, 'digit'); %Testing for digits
AlphaTest = isstrprop(NumInput, 'alpha'); %Testing for letters
AlphaNumTest = isstrprop(NumInput, 'AlphaNum'); %Testing for combination
if NumTest(1,:) == 1 NumInput = str2double(NumInput); disp(NumInput); elseif AlphaTest(1,:) == 1 fprintf('Please enter only a numeric value.\n\n'); NumInput = (''); continue elseif AlphaNumTest(1,:) == 1 NumTest = isstrprop(NumInput, 'digit'); NumsOnly = find(NumTest); NumInput = str2double(NumInput(NumsOnly)); fprintf('\nOnly numerical values can be entered;\nUsing %1.5g\n\n', NumInput); else error('Please enter only a numerical value:'); NumInput = (''); endend
Surely these is a better way?
Best Answer