Filter löschen
Filter löschen

Error using logical operators on a string input

6 Ansichten (letzte 30 Tage)
Madalena Francisco
Madalena Francisco am 15 Jan. 2023
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 Kommentare
the cyclist
the cyclist am 15 Jan. 2023
What are you typing into the input prompt?
1
or
'1'
or something else?
Madalena Francisco
Madalena Francisco am 15 Jan. 2023
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!

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 15 Jan. 2023
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 Kommentare
Walter Roberson
Walter Roberson am 15 Jan. 2023
if ~ismember(b, {'1', '2')})
Or use menu() or questdlg()
Madalena Francisco
Madalena Francisco am 15 Jan. 2023
Ok thanks a lot I will analyse what you sugested!!

Melden Sie sich an, um zu kommentieren.


Sulaymon Eshkabilov
Sulaymon Eshkabilov am 15 Jan. 2023
Bearbeitet: Sulaymon Eshkabilov am 15 Jan. 2023
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 Kommentare
Walter Roberson
Walter Roberson am 15 Jan. 2023
Remember that '1'==1 is false.
Madalena Francisco
Madalena Francisco am 15 Jan. 2023
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by