Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

if/else/then gui?

Asked by James Hendren on 17 Jul 2013

So in my gui I have 3 popup menus each with 2 selections. I need to base code on each selection. for example

    if get(popupmenu1.handles,'value')==1
    (insert code here) for value of popupmenu 2
    (insert code here) for value of popupmenu 3
    Inesrt my own function to be run dependent on the three values

I would need code that would get the value from popupmenu2, and then 3. Then I would run my a specific fitting procedure for the selections. By combinatorics there should be 8 combinations, and I would like my code to be simple. Can anyone help with that? Thanks in advance

0 Comments

James Hendren

Products

No products are associated with this question.

1 Answer

Answer by Image Analyst on 17 Jul 2013
Accepted answer

What's the difficulty? You already know how to get the selections:

popupValue1 = get(popupmenu1.handles,'value');
popupValue2 = get(popupmenu2.handles,'value');
popupValue3 = get(popupmenu3.handles,'value');

And you know how (or should know how) to construct if/then/else blocks, so where are you having trouble?

if popupValue1==1 && popupValue2==1 && popupValue3==1
  % Some code
elseif popupValue1==1 && popupValue2==1 && popupValue3==2
  % Some code
elseif popupValue1==1 && popupValue2==2 && popupValue3==1
  % Some code
elseif popupValue1==1 && popupValue2==2 && popupValue3==2
  % Some code
elseif popupValue1==2 && popupValue2==1 && popupValue3==1
  % Some code
elseif popupValue1==2 && popupValue2==1 && popupValue3==2
  % Some code
elseif popupValue1==2 && popupValue2==2 && popupValue3==1
  % Some code
elseif popupValue1==2 && popupValue2==2 && popupValue3==2
  % Some code
end

There's your eight possible cases, but I think you already knew that, so please clarify.

2 Comments

Image Analyst on 17 Jul 2013

James comment moved here:

I wanted to know if there was a cleaner way to do it...

So it would retrieve the values of 1,2,and 3 without having to type all the steps per se

I was wondering if it could be done in 3 steps rater than 8

Image Analyst on 17 Jul 2013

You can't retrieve them all without retrieving them all, with get(). I think 3 ifs with ifs nested even deeper is a lot more complicated than the straightforward and easy-to-understand way I gave.

Image Analyst

Contact us