Error using logical operators on a string input

6 次查看(过去 30 天)
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 个评论
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)

Sulaymon Eshkabilov
编辑: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 个评论
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