Matlab GUI popup menu

3 views (last 30 days)
Ege
Ege on 5 Jan 2015
Answered: Image Analyst on 8 Jan 2015
Hi I'm new to GUI so sorry if it's so obvious.
I want to use a popup menu to select which dataset to load in my program. In the popup menu it simply says Dataset-1 and Dataset-2 for contents. I use following:
function popup_selectds_Callback(hObject, eventdata, handles)
a=get(handles.popup_selectds,'value');
switch a
case 1
load('dataset.mat')
case 2
load('dataset2.mat')
end
guidata(hObject, handles);
It does not give any errors and when I debug it it works but it does not work when I do in on the GUI. What's wrong with it?
Thank you.
  1 Comment
Geoff Hayes
Geoff Hayes on 8 Jan 2015
Ege - It isn't obvious to me what is wrong with your code. In fact, if I copy and paste it into a sample GUI, then the correct case is executed given the selection in the popup menu.
As an aside, you could try replacing
a=get(handles.popup_selectds,'value');
with
a=get(hObject,'value');
as hObject will also be the handle to the popup (and should be identical to handles.popup_selectds).
Also, are you sure that the popup_selectds_Callback will fire? Is the name/tag of your popup menu still popup_selectds?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 8 Jan 2015
When you call load(), it "poofs" the variables into the workspace of the function. When that function exits, all those variables are lost. If you want to retain them, so that you can use them outside the callback function, you'll have to make them global, or attach them to a new field(s) that you create on the handles structure. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

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!