Matlab GUI: Referencing Pop-up Menu hObject outside of callback funciton

1 view (last 30 days)
Hello All,
I am currently working on a GUI for automated data analysis, and I'm struggling a bit at the moment with figuring out how to reference one of my pop-up menu's "hObject" properties outside of the popupmenu1_callback function... the objective being the following:
If a user presses a button on the GUI, a separate GUI will initialize depending on the "value" of the pop-up/drop-down menu.
Within the pop-up menu callback, I can get away with using the following method to alter data:
str1 = get(hObject,'String');
val1=get(hObject,'Value');
switch str1{val1};
case 'Event 1' % user selects Event #1...
%%%%%%%%Code %%%%%%%%%%%%%
case 'Event 2' % user selects Event #2...
%%%%%%%%Code %%%%%%%%%%%%%
end
However I cannot figure out how to get the 'String' and 'Value' properties from the Pop-up menu's hObject OUTSIDE of the callback (other places within the GUI code). I've even tried to generate variables inside the case structure to tell the code later what "event" is selected, but to no avail!
Any advice on how to do this would be greatly appreciated!

Answers (1)

Geoff Hayes
Geoff Hayes on 13 Sep 2014
Colin - you've added the tag guide to your question so you must be using GUIDE to develop your GUI. In your push button callback, the third input will be the handles structure which includes the handles to all widgets and to user-defined data. So you can use this structure to access the properties of the popup menu. If we assume that your popup menu is named (or tagged) as popupmenu1 and that your push button is named (or tagged) as pushbutton1 then you can do the following
function pushbutton1_Callback(hObject, eventdata, handles)
str1 = get(handles.popupmenu1,'String');
val1 = get(handles.popupmenu1,'Value');
switch str1{val1};
case 'Event 1' % user selects Event #1...
%%%%%%%%Code %%%%%%%%%%%%%
case 'Event 2' % user selects Event #2...
%%%%%%%%Code %%%%%%%%%%%%%
end
In the above, we do exactly what you have shown for the callback of your popup menu, but from within the pushbutton callback using the handles structure. Try the above and see what happens!

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!