Error using logical operators on a string input

조회 수: 13 (최근 30일)
Madalena Francisco
Madalena Francisco 2023년 1월 15일
댓글: Madalena Francisco 2023년 1월 15일
Hi! I want accept if the user input a string, for example: wefef, so i put my input with ('s').
Then i create i while loop to check if that input it´s different from 1 our 2, if its empty and if it´s a string, in this case I want to appear a error mesage.
My code isn´t working, it´s giving me error: Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.
Can you help me? Thanks a lot!
elseif b==2
a1= sprintf('If you want to advance in the program, press 1. If you want to go back to the previous menu, press 2: ');
b1=(input(a1,'s'));
while (b1 ~= 1) && (b1 ~= 2) && (isempty(b1) || isstring(b1))
clc
errordlg ({'Incorrect option entry.','Please press Ok and enter the desired value in the Command Window.'})
a1= sprintf('If you want to advance in the program, press 1. If you want to go back to the previous menu, press 2: ');
b1=(input(a1,'s'));
end
  댓글 수: 2
the cyclist
the cyclist 2023년 1월 15일
What are you typing into the input prompt?
1
or
'1'
or something else?
Madalena Francisco
Madalena Francisco 2023년 1월 15일
I want that the user put everything that he want, but I want to check if its what I want:
it´s different from 1 our 2, if its empty and if it´s a string.
I type 1 without the '' to the input prompt, if i want to proceed in the program.
Thank u!

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

답변 (2개)

Walter Roberson
Walter Roberson 2023년 1월 15일
input with the 's' option returns a character vector, possibly with multiple characters, possibly empty. isstring() for it will always be false but ischar() would be true.
Remember that character vectors are vectors. If the user enters 21 then that would be ['2' '1'] and then comparing to 1 would be ['2'==1, '1'==1] which would be a vector result [false false] but && requires that the sides return scalar. Notice that '1'==1 is false because '1' has character code 49 which is not 1.
And remember that []==1 would return an empty logical vector but && needs a scalar
Use strcmp to compare character vectors, and compare them against characters not against numeric values (unless you know what you are doing)
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 1월 15일
if ~ismember(b, {'1', '2')})
Or use menu() or questdlg()
Madalena Francisco
Madalena Francisco 2023년 1월 15일
Ok thanks a lot I will analyse what you sugested!!

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 15일
편집: Sulaymon Eshkabilov 2023년 1월 15일
Here is the corrected part of your code.
Note that you'd need to get a single value logical in order to compare the two different logicals, and thus, use any()
...
a1= sprintf('If you want to advance in the program, press 1. If you want to go back to the previous menu, press 2: ');
b1=(input(a1,'s'));
while any(b1 ~= 1) && any(b1 ~= 2) && isempty(b1) || isstring(b1)
clc
errordlg ({'Incorrect option entry.','Please press Ok and enter the desired value in the Command Window.'})
a1= sprintf('If you want to advance in the program, press 1. If you want to go back to the previous menu, press 2: ');
b1=(input(a1,'s'));
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2023년 1월 15일
Remember that '1'==1 is false.
Madalena Francisco
Madalena Francisco 2023년 1월 15일
I tried to create a version of my code:
What you all think?
It´s working this way!
I used str2double to convert the numbers in string form to numbers.And if the user entered for example: 'ddwf', str2double would not be able to convert it to a number, so it would be in NaN format (Not a Number), so in the while loop I guaranteed the conditions I wanted, and I think that what I was failing was the fact that I didn't properly use the parentheses in the other code:
Thanks a lot!
a1= sprintf('If you want to advance in the program, press 1. If you want to go back to the previous menu, press 2: ');
b1=str2double(input(a1,'s'));
while ((b1 ~= 1) && (b1 ~= 2)) || (isempty(b1) || isnan(b1))
clc
errordlg ({'Incorrect option entry.','Please press Ok and enter the desired value in the Command Window.'})
a1= sprintf('If you want to advance in the program, press 1. If you want to go back to the previous menu, press 2: ');
b1=str2double(input(a1,'s'));
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by