Should I declare GUI components in a struct or as separate variables?
Show older comments
function script6()
fig = figure('units', 'pix', 'position', [350, 200, 400, 170], 'menubar' , 'none');
popmenuh = uicontrol('style', 'popupmenu', 'units', 'normalized', 'position', [0.3, 0.7, 0.4, 0.1], 'String', {'Option 1', 'Option 2'}, 'FontWeight', 'bold', 'FontSize', 12, 'Callback', @popmenuh_Callback);
text1 = uicontrol('style', 'text', 'string', 'Option 1: 0', 'FontWeight', 'bold', 'FontSize', 12, 'units', 'normalized', 'position', [0.6, 0.1, 0.25, 0.15]);
text2 = uicontrol('style', 'text', 'string', 'Option 2: 0', 'FontWeight', 'bold', 'FontSize', 12, 'units', 'normalized', 'position', [0.1, 0.1, 0.25, 0.15]);
cOne = 0;
cTwo = 0;
function popmenuh_Callback(source, event)
currentValue = get(popmenuh, 'Value');
switch currentValue
case 1
cOne = cOne + 1;
case 2
cTwo = cTwo + 1;
end
CONE = strcat('Option 1:', num2str(cOne));
CTWO = strcat('Option 2: ', num2str(cTwo));
set(text1, 'String', CONE) ;
set(text2, 'String', CTWO);
end
end
function [] = GUI_7()
S.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','GUI_7',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','tex',...
'unit','pix',...
'position',[10 15 280 20],...
'backgroundcolor',get(S.fh,'color'),...
'fontsize',12,'fontweight','bold',...
'string','OPTION 1: 0 OPTION 2: 0');
S.pp = uicontrol('style','pop',...
'unit','pix',...
'position',[10 60 280 20],...
'backgroundc',get(S.fh,'color'),...
'fontsize',12,'fontweight','bold',...
'string',{'option 1';'option 2'},'value',1);
S.CNT = [0 0]; % Holds the number of times each option has been called.
set(S.pp,'callback',{@pp_call,S}); % Set the callback.
function [] = pp_call(varargin)
S = varargin{3}; % Get the structure.
P = get(S.pp,'val'); % Get the users choice from the popup.
S.CNT(P) = S.CNT(P) + 1; % Increment the counter.
set(S.tx, 'string', sprintf('OPTION 1: %i OPTION 2: %i', S.CNT));
set(S.pp,'callback',{@pp_call,S}); % Save the new count.
I'm just getting started with GUIs in MATLAB and I have been encountering mainly two types of approaches to creating GUIs. The first one (script6) seems very straightforward by declaring each component of the GUI as a separate variable, while the other one (GUI_7) creates a struct where it stores all components of the GUI. Which approach would be more efficient? I know that in this case it might not make a big difference, but I am just curious to know which approach is more effective when creating more complex GUIs. Thank you.
Accepted Answer
More Answers (0)
Categories
Find more on Web Services 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!