Hello,
I am trying to validate a user input as numeric while forcing the user to repeat if it is not. I currently have this working well as long as the user does not input a letter that is non numeric. I would like it to prompt the user again if they input a string instead of a number. Any help is appreciated.
Thanks
my input looks like
array(r, c) = str2num(input(['Digit for position ', num2str(c), ': '], 's'));
while array(r, c) > 6 || array(r, c) < 0
GUESS(r, c) = str2num(input(['Digit for position ', num2str(cc), ': '], 's'));
end

 채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 27일
편집: Walter Roberson 2020년 7월 27일

0 개 추천

array(r, c) = str2double(input(['Digit for position ', num2str(c), ': '], 's'));
while isnan(array(r,c)) || array(r,c) ~= fix(array(r,c)) || array(r, c) < 0 || array(r, c) > 6
array(r, c) = str2double(input(['Digit for position ', num2str(cc), ': '], 's'));
end
or
array(r, c) = str2double(input(['Digit for position ', num2str(c), ': '], 's'));
while ~ismember(array(r,c), 0:6)
array(r, c) = str2double(input(['Digit for position ', num2str(cc), ': '], 's'));
end

댓글 수: 3

Scott Tobin
Scott Tobin 2020년 7월 27일
Okay, thank you.
However, I know have the issue of it interpreting the input as a string. Inputing 'w' yields unrecognized funciton or variable 'w'
Scott Tobin
Scott Tobin 2020년 7월 27일
Actually that was on me with a typo but I do have...
Unable to perform assignment because the size of the left side is 1-by-1 and the size
of the right side is 0-by-0 for the array in the loop
Scott Tobin
Scott Tobin 2020년 7월 27일
Ope spotted was supposed to be str2double not str2num in the second. Appreciate the help I haven't seen it done in that manner before thanks for the tip

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2020년 7월 27일

편집:

2020년 7월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by