MATLAB Answers

Should I declare GUI components in a struct or as separate variables?

4 views (last 30 days)
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;
CONE = strcat('Option 1:', num2str(cOne));
CTWO = strcat('Option 2: ', num2str(cTwo));
set(text1, 'String', CONE) ;
set(text2, 'String', CTWO);
function [] = GUI_7()
S.fh = figure('units','pixels',...
'position',[300 300 300 100],...
S.tx = uicontrol('style','tex',...
'position',[10 15 280 20],...
'string','OPTION 1: 0 OPTION 2: 0');
S.pp = uicontrol('style','pop',...
'position',[10 60 280 20],...
'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.


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 15 Apr 2018
Edited: Stephen Cobeldick on 15 Apr 2018
It depends on how large and complex the GUI will be, and what you want to do with the handles. For a simple GUI using nested functions then individual variables can be easy and intuitive. See my FEX submission iregexp for an example:
It is certainly possible to write quite a complex GUI with individual handle variables and nested functions: I wrote this mostly just to entertain myself:
Any more complex than that though and you should definitely put the handles into a structure, or even better defining a class. This is because with a more complex GUI then you will want to pass multiple handles as arguments, or store them using persistent, in which case putting them into one structure make that much simpler. It also makes handling them less verbose, which makes the code easier to follow. Sometimes I put the figure creation in its own local function and simply return one structure containing all of the required handles that the GUI will require, for example:
Do whatever makes your code clearest, and easiest to understand.


Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!