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 件のコメント

Sean de Wolski
Sean de Wolski 2011 年 3 月 21 日
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
Walter Roberson
Walter Roberson 2011 年 3 月 21 日
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 ExchangeLoops 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