|
"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
|