필터 지우기
필터 지우기

Use User Input as a Number to be Checked in If/Else Statement

조회 수: 57 (최근 30일)
Jon
Jon 2022년 12월 20일
댓글: Voss 2022년 12월 20일
Hello,
I'm having issues figuring this out, even though it seems it should be relatively simple.
I have the user input a number, and that value is then used to determine which cell in a vector is used for a different command.
Based on what I gathered, it seems that User Input is always a string (I could be mistaken), and not considered a numeric value, rather a character.
I have something like this:
prompt = 'Which Day?'
take1 = str2double(input(prompt)) % Take the number the user inputs and use it as a numeric number
if take1==1 || take1==2 || take1>N
% Here I want to check if the Number the user inputs is a certain value
% It can't be 1 or 2 and had to be less than a certain value
disp('Pick Again')
% Then I'll have another prompt for them to choose a different value
elseif -1^(take1*2)==1
% Here I want to make sure the user inputs an integer (no decimals)
% If this is true, I'll use the inputted number
else
disp('Pick Again')
% Another prompt here too
end
I keep on getting error messages, which I think lies in how I'm using the inputted value.
I might end up doing another check to be sure the user does not input another value.
Any help is appreciated.
Also, on a side note, is it possible to have these if/else statement loop back to the initial prompt?
So rather than in each if or elseif section I create a new UI prompt, the script goes back up to the first 'Which day?'.
Thanks.
  댓글 수: 1
Stephen23
Stephen23 2022년 12월 20일
편집: Stephen23 2022년 12월 20일
" it seems that User Input is always a string (I could be mistaken)"
You are mistaken: if you do not use the 's' option then its output depends much on what the user types. As the documentation explains, the user can also use INPUT() to enter expressions, which could lead to unexpected side effects.
This is why experienced MATLAB users strongly recommend that for robustness and security you should use the 's' option and STR2DOUBLE() if the expected data are scalar numeric:
take1 = str2double(input(prompt,'s'));
Writing better code is simple, when you know how.

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

채택된 답변

Voss
Voss 2022년 12월 20일
편집: Voss 2022년 12월 20일
"it seems that User Input is always a string"
The value returned from the input function would be a character vector if you used the 's' option. Without the 's' the returned value will be numeric if the user enters a number.
You are expecting a number, and you are not using 's', which is consistent; you just need to avoid the call to str2double. (str2double with a numeric argument returns NaN.)
"is it possible to have these if/else statement loop back to the initial prompt?"
Yes. Use a while loop.
prompt = 'Which Day?'
while true
take1 = input(prompt); % Take the number the user inputs and use it as a numeric number
if take1==1 || take1==2 || take1>N
% Here I want to check if the Number the user inputs is a certain value
% It can't be 1 or 2 and had to be less than a certain value
disp('Pick Again')
% Then I'll have another prompt for them to choose a different value
elseif rem(take1,1) == 0 % (-1)^(take1*2)==1
% Here I want to make sure the user inputs an integer (no decimals)
% If this is true, I'll use the inputted number
break % exit the while loop
else
disp('Pick Again')
% Another prompt here too
end
end
Note that this considers 0 and negative integers to be valid inputs, and it will throw an error for non-scalar inputs.
  댓글 수: 2
Jon
Jon 2022년 12월 20일
Thank you for your answer to both of my questions.
Also, that's a nicer and neater way to check if it's an integer, probably less expensive too.
Voss
Voss 2022년 12월 20일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by