GUI with two popup menus
Show older comments
Dear all,
I am new to GUI programming and I have tried to figure out for some time how to use two popup menus in a GUI.
Suppose popup menu 1 specifies a set of countries and popup menu 2 specifies a set of variables common to all countries. I would like to plot all possible country/variable pairs.
Below I show my code which does not work (potentially also because I do not appropriately pass the data between functions "popup_menu1_Callback" and "popup_menu2_Callback").
Any hints or suggestions would be great, many thanks!
Here is the example-code:
function example_gui
% SIMPLE_GUI2 Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.
% Create and then hide the GUI as it is being constructed.
f = figure('Visible','off','Position',[260,500,800,485]);
% Construct the components.
hplot = uicontrol('Style','pushbutton','String','Figure','Position',[615,420,100,25],'Callback',{@plot_Callback});
hpopup = uicontrol('Style','popupmenu','String',{'US','Japan'},'Position',[615,260,100,25],'Callback',{@popup_menu1_Callback});
htext = uicontrol('Style','text','String','Country','Position',[615,300,100,15]);
gpopup = uicontrol('Style','popupmenu','String',{'GDP','Trade Balance'},'Position',[615,180,100,25],'Callback',{@popup_menu2_Callback});
gtext = uicontrol('Style','text','String','Indicator','Position',[615,220,100,15]);
ha = axes('Units','pixels','Position',[50,60,500,385]);
align([hplot,gtext,htext,hpopup,gpopup],'Center','None');
% Change units to normalized so components resize automatically.
set([hplot,gtext,htext,hpopup,gpopup],'Units','normalized');
% Generate the data to plot.
vars=evalin('base','vars') % matrix with dimesnion 4x100
% Assign the GUI a name to appear in the window title.
set(f,'Name','Simple GUI')
% Move the GUI to the center of the screen.
movegui(f,'center')
% Make the GUI visible.
set(f,'Visible','on');
function popup_menu1_Callback(hObject,source,eventdata)
global data
% Determine the selected data set.
str = get(hObject, 'String');
val = get(hObject, 'Value');
% Set current data to the selected data set.
switch str{val};
case 'US'
data = vars(1:2,:)
case 'Japan'
data = vars(3:4,:)
end
end
function popup_menu2_Callback(hObject,source,eventdata)
global data
% Determine the selected data set.
str = get(hObject, 'String');
val = get(hObject, 'Value');
% Set current data to the selected data set.
switch str{val};
case 'GDP'
data = data(1,:)
case 'Trade Balance'
data = data(2,:)
end
end
function plot_Callback(source,eventdata)
plot(data);
end
end
2 Comments
Stephen23
on 4 Sep 2014
kanimbla: If someone has written an answer that solves your problem, then it would be polite to "accept" it... I am sure that this would be appreciated.
Note that global variables are almost always a bad idea:
Instead use nested functions, and/or passing variables as inputs and outputs.
kanimbla
on 4 Sep 2014
Accepted Answer
More Answers (1)
kanimbla
on 4 Sep 2014
0 votes
Categories
Find more on Variables 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!