Mutually Exclusive Booleans in a single Switch

Dear Matlab Commnunity
I have 4 mutually exclusive Boolean variables called "HH", "LH", "SO" and "CB". I wish an appropriate piece of code (concatenate string array for a plot legend) to run in the event any possible case is true. I currently have four separate 'if' statements;
legendtext = [];
if (HH == 1)
legendtext = [legendtext, {'Heavy Holes'}];
end
if (LH == 1)
legendtext = [legendtext, {'Light Holes'}];
end
if (SO == 1)
legendtext = [legendtext, {'Spin Orbit'}];
end
if (CB == 1)
legendtext = [legendtext, {'Electrons'}];
end
in the event of all four booleans being false (== 0) legendtext is an empty array, and in the event all four booleans are true (= 1) legendtext is a 1x4 cell.
I tried using a 'switch' statement since I was under the pretence that while 'if' statements jump to the end when the criteria is met, 'switch' statements go through all cases;
legendtext = [];
switch 1
case HH
legendtext = [legendtext, {'Heavy Holes'}];
case LH
legendtext = [legendtext, {'Light Holes'}];
case SO
legendtext = [legendtext, {'Spin Orbit'}];
case CB
legendtext = [legendtext, {'Electrons'}];
end
however I learned that unlike in C, matlab terminates when a case is matched!
So my question is: is there a way of getting the switch (or any other) statement to scan each mutually exclusive Boolean without using 4 separate if statements? (like a continue statement in a while loop)

댓글 수: 4

What's wrong with 4 if-statements?
Doug Hull
Doug Hull 2011년 3월 21일
If they are mutually exclusive, how can they all four be true as you stated?
Robert Ward
Robert Ward 2011년 3월 21일
Apologies; I mean independent of each other rather than mutually exclusive. They are mutually exclusive in that if one is true it doesn't affect the others.
Andrew Newell
Andrew Newell 2011년 3월 21일
I agree with @Sean de - if statements seem like the simplest choice for this particular problem.

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

답변 (1개)

Matt Fig
Matt Fig 2011년 3월 21일

0 개 추천

Although there appears to be nothing wrong with your multiple IF statements (except that perhaps the comparison to 1 is not necessary), why not use indexing?
legendtext2 = {'Heavy Holes' 'Light Holes' 'Spin Orbit' 'Electrons'};
legendtext2 = legendtext2([HH,LH,SO,CB])

댓글 수: 3

Robert Ward
Robert Ward 2011년 3월 21일
the if statements do indeed work OK however are cumbersome and would like to find another concise and efficient solution.
While the indexing technique does work I have additional statements when each is true (such as plot), so it doesn't completely solve the problem.
Thank you for your help.
Rob
Use function handles in connection with indexing.
Matt Fig
Matt Fig 2011년 3월 21일
Of course you completely left out that there was other code in your selection statements. It would make things easier on those who are trying to help you if you _specified_ the problem before posting....
Since I am not sure what other types of things, besides a call to PLOT and the assignment of a legend string, is going within your selections, I cannot help further. Perhaps if you detailed the problem.....

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2011년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by