Creating Graphical User Interfaces |
Files for Programmatically Created GUIs
When you create a GUI programmatically, these are two approaches you can use to structure your GUI files:
Single M-File Using GUIDE as a Model
In this approach, the file is ordered in the following sections:
help
command.
This is a template for such a file.
function mygui(arg1,arg2,...) % mygui These comments are displayed at the command line % in response to the help command. % Hide the GUI as it is being constructed. f = figure('Visible','off'); % Construct the components. hb = uicontrol('Style','pushbutton','String','Plot',... 'Tag','pushbutton',...); ha = axes('Tag','axes',...); ... % Generate the structure of handles. handles = guihandles; % Associate callbacks with components. set(hb,'Callback',{@pushbutton_callback,handles,...},... 'ButtonDownFcn',... {@pushbutton_buttondownfcn,handles,...},...); set(ha,'ButtonDownFcn',{@axes_buttondownfcn,handles,...},... 'CreateFcn',{@axes_createfcn,handles,...},...); ... % Initialize the GUI. set(f,'Position',[100,100,300,500],'Visible','on',...); ... % Callbacks for mygui function pushbutton_callback(source,eventdata,handles,...) ... function pushbutton_buttondownfcn(source,eventdata,handles,...) ... function axes_buttondownfcn(source,eventdata,handles,...) ... function axes_createfcn(source,eventdata,handles,...) ...
Single M-File Using Nested Funtions
In this approach, the file is ordered in the following sections. For general information about nested functions, see Nested Functions in the MATLAB documentation.
help
command.
This is a template for such a file.
function mygui(arg1,arg2,...) % mygui These comments are displayed at the command line % in response to the help command. % Hide the GUI as it is being constructed. f = figure('Visible','off'); % Construct the components hb = uicontrol('Style','pushbutton','String','Plot',... 'Tag','pushbutton',... 'Callback',{@pushbutton_callback,in_arg1,...},... 'ButtonDownFcn',... {@pushbutton_buttondownfcn,in_arg1,...},...); ha = axes('Tag','axes',... 'ButtonDownFcn',{@axes_buttondownfcn,in_arg1,...},... 'CreateFcn',{@axes_createfcn,in_arg1,...},...); ... % Initialize the GUI set(f,'Position',[100,100,300,500],'Visible','on',...); ... % Callbacks for mygui. These callbacks automatically have % access to handles ha and hb because they are nested at a % lower level. function pushbutton_callback(source,eventdata,in_arg1,...) ... end function pushbutton_buttondownfcn(source,eventdata,... in_arg1,...) ... end function axes_buttondownfcn(source,eventdata,in_arg1,...) ... end function axes_createfcn(source,eventdata,in_arg1,...) ... end end
GUIDE M-Files and FIG-Files | Associating Callbacks with Components |
© 1994-2005 The MathWorks, Inc.