problems using && with dropdown list result

hi!
S={'NH4Cl'; 'MgCl2'; 'CaCl2'; 'ZnCl2'; 'ZnSO4'; 'NaCl'};
>> str={'Choose the combinations of salts available'};
>> result=listdlg('Promptstring',str, 'ListSize', [100,100], 'ListString', S, 'SelectionMode', 'multiple');
>> if result==1&&result==3
disp('hei')
end
the error I get: Operands to the || and && operators must be convertible to logical scalar values. Donnt understand because the output from result is scalar and is should be possible to use && togehter with scalar outputs..

댓글 수: 4

Adam
Adam 2019년 9월 24일
편집: Adam 2019년 9월 24일
You explictly set selection mode to 'multiple' so unless you only select one item it won't be a scalar. And if it was a scalar then
result==1&&result==3
would clearly always evaluate to false anyway! Maybe you want something more like:
all( ismember( [1,3], result ) )
Muazma Ali
Muazma Ali 2019년 9월 24일
편집: Muazma Ali 2019년 9월 24일
does "all" here mean that both options have to be included in the result vector? if thats so, that s what I need :)
Muazma,
The problem with your code is clear to me (and Adam). You have allowed the user to select multiple items. For example, they might select 'NH4Cl' and 'CaCl2'. In this case,
result = [1 3]
and you are trying to evaluate
[1 3]==1 && [1 3]==3
which is
[1 0] && [0 1] % these are logical vectors
These are not allowable operands for the short-circuit logical operators. The operands must each be a single scalar value.
What is not clear to me is what you want to happen in your if statement. Perhaps you could explain this more completely.
Muazma Ali
Muazma Ali 2019년 9월 24일
Ok maybe I should write the question in another way; how can I include the whole vector in the if statement? Because i need both of the values, 1 and 3, to be true as options for the next statements

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

 채택된 답변

the cyclist
the cyclist 2019년 9월 24일

0 개 추천

Here is what I think you want, based on your most recent comment:
if all(ismember([1 3],result))
disp('hei')
end
The if statement will be entered if the user's selections include 'NH4Cl' and 'CaCl2'.

추가 답변 (1개)

the cyclist
the cyclist 2019년 9월 24일

0 개 추천

My best guess as to what you want is
if any(ismember(result,[1 3]))
disp('hei')
end
The if statement will be entered if the user's selections include 'NH4Cl' or 'CaCl2' (or both).

댓글 수: 2

Muazma Ali
Muazma Ali 2019년 9월 24일
I want both of them as the answer, in other words proceed with some specific statements if the user has both chosen 1tem 1 n 3 as options
Maybe the OP wants all instead of any. I.e all the selections must be NH4Cl or CaCl2
if all(ismember(result, [1 3]))

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

카테고리

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

질문:

2019년 9월 24일

편집:

2019년 9월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by