MATLAB Answers

Wildcard or multiple conditions on switch/case?

314 views (last 30 days)
Hello,
Is there a way to deal with multiple conditions in switch/case? For example (this is just example):
  • Meat could be chicken pork beef lamb
  • Veggie could be beans peas corn
  • Fruit could be apple pear blueberry
Looking for a way to then select for each possible combination. Is the easiest way just nested switch/case? Or is there something more elegant? Is there an easy way to use case wildcards (not great for this example)?
Thanks!
Doug

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 24 Apr 2018
Select for each possible combination? Do you mean something like recognizing that a given cell array includes one entry from each of the three categories? Or do you mean enumerating all of the valid combinations ?
Douglas Anderson
Douglas Anderson on 24 Apr 2018
I need to call three different functions, depending on the combination. Each of the functions then may have different arguments passed to them. (These are functions I have written).
One thing that I had thought of was making some kind of "code" for the options, like "Beef = 1", "Pork = 2", and then "Beans = 10", "Peas = 20", and "Apple = 100", etc. So Apple Peas Beef would be 121. Couldn't figure out how to parse this so that switch could deal with ANY one with Apple, i.e., 1** where * is any of the other veggies and fruits. That's the wild card I was thinking of.
Thanks!
Douglas Anderson
Douglas Anderson on 24 Apr 2018

Actually this is the code snippet:

handles.column_flag     = 1; % 1 = Solid, 2 = Decked
handles.hole_flag       = 10; % 10 = Standard, 20 = Long First Row
handles.row_flag        = 100; % 100 = Single Row, 200 = Multi Row, 300 = Progressive
handles.opening_flag    = 1000; % 1000 = Left, 2000 = Right, 3000 = Middle

So the combination of Decked, standard, Multi, Middle (summed) is 3212

There aren't a lot of choices for each flag, but there are a lot of combinations, each leading down a different code path!

Thank you for your thoughts.

Doug

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 18 Oct 2019
I'm not sure how switch / case fits into this. You don't need it (unless this is part of a homework assignment and it requires you to use a switch / case.) Let's define the food groups. I'm using string arrays but you could use numbers.
meat = ["chicken"; "beef"; "pork"; "lamb"];
veggie = ["beans"; "peas"; "corn"];
fruit = ["apple"; "pear"; "blueberry"];
Use ndgrid to generate the 3-D arrays where combining the same element of each of the three arrays gives you one combination.
[M, V, F] = ndgrid(meat, veggie, fruit);
Combine them into a 2-D list with each row representing one combination.
possibleDinners = [M(:), V(:), F(:)];
What to eat today?
whichDinner = randi(size(possibleDinners, 1));
dinnerTonight = possibleDinners(whichDinner, :)

  3 Comments

Douglas Anderson
Douglas Anderson on 19 Oct 2019
Thank you, Steven!
I had given up, and made kind of a kludge. This makes sense!
Doug
Walter Roberson
Walter Roberson on 19 Oct 2019
I am not sure how this deals with "I need to call three different functions, depending on the combination." ?
Douglas Anderson
Douglas Anderson on 19 Oct 2019
Hello Walter,
There are three different functions that I have written. Which function is called depends on the particular combination, and then the appropriate variables are passed to the called function.
In the "meat/veggie/fruit" example, it could be whether on the grill or on the oven, and then how to do each one. In my case it has to do with particular designs for blasting, which can be complicated.
By the way, thank you for all the help you have given in the past!
Doug

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 24 Apr 2018
Choice='chicken';
switch Choice
case {'chicken','pork','beef'}
disp('meat');
case {'bean','pea'}
disp('veggie')
case {'apple','pear'}
disp('fruit');
otherwise
disp('no category');
end

  5 Comments

Show 2 older comments
David Walwark
David Walwark on 18 Oct 2019
Was helpful for trying to find out how to make a switch-case handle what I would normally assume I can use an 'OR' bar for.
So this is not really an answer to this question, but google lead me here to answer the question I had, so I am doing my part for the SEO by reinforcing it with more keywords.
The snarky question I had would be 'Why can't I use OR statements in a switch case?'. I really just wanted the switch case to be case insensitive by including some of the possibilities I knew would come up in my spectroscopic context: 'energy','eV','ev','EV'...
Walter Roberson
Walter Roberson on 18 Oct 2019
switch(lower(VARIABLE))
and then you do not have to worry about supplying 'eV' separate from 'ev' and so on.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!

Translated by