Thread Subject: Help with GUI popup menus

Subject: Help with GUI popup menus

From: Stephane

Date: 1 Dec, 2008 17:56:02

Message: 1 of 9

Hi,

I cannot find how to program a dynamic menu, in which I can narrow down the selection. For instance, in a popupmenu1, select A or B. Then in an adjacent popupmenu2, provide a list of choices according to A or B (eg. A1, A2, A3 or B1, B2).

That's just like menus on the internet: eg. choose your state (CA, NY, etc) and then it gives a list of cities in that state in another menu.

Does anyone know if this is possible?

Subject: Help with GUI popup menus

From: Husam Aldahiyat

Date: 1 Dec, 2008 18:10:22

Message: 2 of 9

"Stephane " <spoussou@purdue.edu> wrote in message <gh18fi$9tk$1@fred.mathworks.com>...
> Hi,
>
> I cannot find how to program a dynamic menu, in which I can narrow down the selection. For instance, in a popupmenu1, select A or B. Then in an adjacent popupmenu2, provide a list of choices according to A or B (eg. A1, A2, A3 or B1, B2).
>
> That's just like menus on the internet: eg. choose your state (CA, NY, etc) and then it gives a list of cities in that state in another menu.
>
> Does anyone know if this is possible?

You can set the string of a popupmenu in the .m file.

switch get(handle1,'value') %first popupmenu
case 1
set(handle2,'string',{}) %second popupmenu
case 2
set(handle2,'string',{}) %second popupmenu
end

Subject: Help with GUI popup menus

From: ImageAnalyst

Date: 1 Dec, 2008 18:14:46

Message: 3 of 9

On Dec 1, 12:56=A0pm, "Stephane " <spous...@purdue.edu> wrote:
> Hi,
>
> I cannot find how to program a dynamic menu, in which I can narrow down t=
he selection. For instance, in a popupmenu1, select A or B. Then in an adja=
cent popupmenu2, provide a list of choices according to A or B (eg. A1, A2,=
 A3 or B1, B2).
>
> That's just like menus on the internet: eg. choose your state (CA, NY, et=
c) and then it gives a list of cities in that state in another menu.
>
> Does anyone know if this is possible?

-----------------------------------------------------
Stephane:
In the callback function of the first popup (really a drop-down list)
you can just load up the string property of the second popup with
whatever it should be based on the selection of the first popup. Is
this what you meant?
Regards,
ImageAnalyst

Subject: Help with GUI popup menus

From: Stephane

Date: 1 Dec, 2008 18:33:02

Message: 4 of 9

Thank you both for your help.

As you suggest, I tried to exhaustively put all my menu items in each callback functions (using the command "case" so I can cascade accordingly my choices). However, the second menu does not adjust automatically when I run it. Eg. I choose B in first menu, but it still displays A1, A2, A3 in second menu no matter what. I suspect it is because I declared initial lists by default at the very beginning of the file. I do not know how to do otherwise than initializing at the very beginning.

Here is what it looks like for a two-menu choice:

% This is to initialize the menus
list_1 = ['A' 'B'];
list_2 = ['A1' 'A2' 'A3'];

% These are the menus
class_hpopup = uicontrol('Style','popupmenu',...
           'String',{list_1},...
           'Callback',{@menu_1_Callback});
class_hpopup = uicontrol('Style','popupmenu',...
           'String',{list_2},...
           'Callback',{@menu_2_Callback});

% This si to update the second menu as a function of the first menu choice
   function menu_2_Callback(source,eventdata)
         str = get(source, 'String'); val = get(source,'Value'); switch str{val};
         case 'A'
             list_2 = ['A1' 'A2' 'A3'];
         case 'B'
             list_2 = ['B1' 'B2'];
         end
      end

It seems something's not right with that syntax.

Subject: Help with GUI popup menus

From: Steven Lord

Date: 1 Dec, 2008 18:49:47

Message: 5 of 9


"Stephane " <spoussou@purdue.edu> wrote in message
news:gh1aku$m4d$1@fred.mathworks.com...
> Thank you both for your help.
>
> As you suggest, I tried to exhaustively put all my menu items in each
> callback functions (using the command "case" so I can cascade accordingly
> my choices). However, the second menu does not adjust automatically when I
> run it. Eg. I choose B in first menu, but it still displays A1, A2, A3 in
> second menu no matter what. I suspect it is because I declared initial
> lists by default at the very beginning of the file. I do not know how to
> do otherwise than initializing at the very beginning.
>
> Here is what it looks like for a two-menu choice:
>
> % This is to initialize the menus
> list_1 = ['A' 'B'];
> list_2 = ['A1' 'A2' 'A3'];
>
> % These are the menus
> class_hpopup = uicontrol('Style','popupmenu',...
> 'String',{list_1},...
> 'Callback',{@menu_1_Callback});
> class_hpopup = uicontrol('Style','popupmenu',...
> 'String',{list_2},...
> 'Callback',{@menu_2_Callback});
>
> % This si to update the second menu as a function of the first menu choice
> function menu_2_Callback(source,eventdata)
> str = get(source, 'String'); val = get(source,'Value'); switch
> str{val};
> case 'A'
> list_2 = ['A1' 'A2' 'A3'];
> case 'B'
> list_2 = ['B1' 'B2'];
> end
> end
>
> It seems something's not right with that syntax.

1) You overwrote the handle to the first menu with the handle to the second
menu in the lines of code immediately after "These are the menus". Not a
big deal, but it's sometimes nice to have all the handles around
(particularly for debugging.) You may also want to store the handles to the
two menus in the handles structure for your GUI by giving each a unique Tag
property that is a valid struct field name, like 'popupmenu1' or
'popupmenu2'. [Assuming you're using GUIDE or are building the handles
structure yourself at some point.]

2) You've set the callback for the first menu to use the menu_1_Callback
function, but then you wrote the function that actually checks the first
menu's setting as menu_2_Callback.

3) Setting the contents of the list_2 variable inside the menu_2_Callback
has NO EFFECT on either the list_2 variable in the other function, where you
initialized the menu, nor the String property of the second menu which was
initialized using the list_2 variable in the other function (assuming you're
not using nested functions, in which case it would affect the variable in
the other function but still wouldn't "automatically update the String
property of the second menu" or anything like that.) You need to SET the
String property of the second menu inside menu_2_Callback in order to update
the second menu's String property with the new values. This is where
storing the second menu's handle in the handles structure of your GUI comes
in handy. Otherwise you'll need to use something like FINDOBJ to find the
handle of the second menu in order to update it.

4) Instead of using string arrays, I'd probably use a cell array of strings.

list_2 = {'A1', 'A2', 'A3'};

If you want to display strings of different lengths in the menu, you'll need
to use a cell array of strings or pad shorter strings to be the same length
as the longer strings.

--
Steve Lord
slord@mathworks.com

Subject: Help with GUI popup menus

From: ImageAnalyst

Date: 1 Dec, 2008 20:51:25

Message: 6 of 9

On Dec 1, 1:33=A0pm, "Stephane " <spous...@purdue.edu> wrote:
> Thank you both for your help.
>
> As you suggest, I tried to exhaustively put all my menu items in each cal=
lback functions (using the command "case" so I can cascade accordingly my c=
hoices). However, the second menu does not adjust automatically when I run =
it. Eg. I choose B in first menu, but it still displays A1, A2, A3 in secon=
d menu no matter what. I suspect it is because I declared initial lists by =
default at the very beginning of the file. I do not know how to do otherwis=
e than initializing at the very beginning.
>
> Here is what it looks like for a two-menu choice:
>
> % This is to initialize the menus
> list_1 =3D ['A' 'B'];
> list_2 =3D ['A1' 'A2' 'A3'];
>
> % These are the menus
> class_hpopup =3D uicontrol('Style','popupmenu',...
> =A0 =A0 =A0 =A0 =A0 =A0'String',{list_1},...
> =A0 =A0 =A0 =A0 =A0 =A0'Callback',{@menu_1_Callback});
> class_hpopup =3D uicontrol('Style','popupmenu',...
> =A0 =A0 =A0 =A0 =A0 =A0'String',{list_2},...
> =A0 =A0 =A0 =A0 =A0 =A0'Callback',{@menu_2_Callback});
>
> % This si to update the second menu as a function of the first menu choic=
e
> =A0 =A0function menu_2_Callback(source,eventdata)
> =A0 =A0 =A0 =A0 =A0str =3D get(source, 'String'); val =3D get(source,'Val=
ue'); switch str{val};
> =A0 =A0 =A0 =A0 =A0case 'A'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0list_2 =3D ['A1' 'A2' 'A3'];
> =A0 =A0 =A0 =A0 =A0case 'B'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0list_2 =3D ['B1' 'B2'];
> =A0 =A0 =A0 =A0 =A0end
> =A0 =A0 =A0 end
>
> It seems something's not right with that syntax.

-----------------------------
Stephane:
You're not retrieving the selection of popup 1 in your code to
menu_2_Callback. And once you fix that, it actually needs to be in
the callback of #1 that you set the entries for #2, not it the
callback for #2. Then you need to send the list_2 string to the popup
#2 via a set() call to the string property (something you omitted
altogether for some reason) because simply setting list_2 does not
automatically load list_2 into the control - you need to do it
manually via the set() command.
Good luck
ImageAnalyst

Subject: Help with GUI popup menus

From: Stephane

Date: 1 Dec, 2008 21:35:03

Message: 7 of 9

It now works using the SET() command.

Thanks a bunch.

Subject: Help with GUI popup menus

From: S?rgio Magalh?es

Date: 12 Feb, 2009 20:43:02

Message: 8 of 9

Hi,

I have imported a number of ASCII files where I want to perform some math stuff. Depending on the number of the imported files I would like that the popup menu showed the name of all files imported. Ex: If I import 10 files I should have 10 itens in the popup menu. Could anyone help me?

Regards,
S?rgio

"Stephane " <spoussou@purdue.edu> wrote in message <gh18fi$9tk$1@fred.mathworks.com>...
> Hi,
>
> I cannot find how to program a dynamic menu, in which I can narrow down the selection. For instance, in a popupmenu1, select A or B. Then in an adjacent popupmenu2, provide a list of choices according to A or B (eg. A1, A2, A3 or B1, B2).
>
> That's just like menus on the internet: eg. choose your state (CA, NY, etc) and then it gives a list of cities in that state in another menu.
>
> Does anyone know if this is possible?

Subject: Help with GUI popup menus

From: Stephane

Date: 4 May, 2009 19:44:02

Message: 9 of 9

Thanks all for your help!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
popupmenu Stephane 1 Dec, 2008 13:00:18
gui Stephane 1 Dec, 2008 13:00:18
rssFeed for this Thread

Contact us at files@mathworks.com