- 1
- 0.5
- .5
- 1e4
- 1e+4
- 1e-4
- 1d4
- 1d+4
- 1d-4
- +1
- -1
- -.5
- .1e1
- +inf
- -inf
- nan
Can isstrprop search for 2 properties?
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
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 = ('');
    end
end
Surely these is a better way?
댓글 수: 2
  Guillaume
      
      
 2019년 4월 8일
				
      편집: Guillaume
      
      
 2019년 4월 8일
  
			It can be done with a regular expression depending on what king of number input you allow/disallow. Are the following inputs allowed:
All of these are valid number representations in matlab. Do you allow all of them. 
Note that if you don't allow NaN, then the simplest may be to simply attempt to convert the  input with str2double and if the result is NaN tell the user that what they entered is invalid.
채택된 답변
  Guillaume
      
      
 2019년 4월 9일
        I would really recommend you use the approach of letting matlab do the conversion for you as I wrote in my comment and as Rik answered. It's really the most flexible and doesn't unnecessarily restrict the user from entering valid inputs.
If you're hell bent on only allowing numbers with no 'e' or 'd' notation, the following regular expression should work. It does more than just checking that the text just contain the allowed characters. It actually checks that the expression makes a valid number:
isvalid = ~isempty(regexp(strinput, '^[+-]?(\d*\.)?\d+$'))
댓글 수: 0
추가 답변 (1개)
  Rik
      
      
 2019년 4월 9일
        I would convert to double type and use ismember to check if all characters are allowed.
Alternatively you could just convert to double with str2double and test if the result is a NaN.
댓글 수: 2
  Guillaume
      
      
 2019년 4월 9일
				
      편집: Guillaume
      
      
 2019년 4월 9일
  
			Yes, this is what I wrote in my comment, and really the simplest way to deal with the problem. This allows all valid number notations and leave matlab to do the validation for you.
I really see no point in forcing the user to write 100000000 instead of 1e9. With the latter, I'm sure I've got the correct number of 0s, with the former, not so sure...
So the code would be:
while true
    strinput = input('Hello!\nWelcome to my unit converter!\nPlease enter a numerical value to convert:\n\n', 's');
    NumInput = str2double(strinput);
    if isfinite(NumInput)  %catch failed conversion (NaN), the user entering NaN or +/- Inf
        break;
    else
        fprintf('\nPlease enter a scalar finite numeric value\n')
    end
end
  Rik
      
      
 2019년 4월 9일
				Thanks, my eyes must have skipped over the suggestion.
Also, that regexp is indeed what the better version of what the ismember should do.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

