Creating Graphical User Interfaces | ![]() ![]() |
Execution Paths in the Application M-File
The application M-file performs different actions depending on what arguments are passed to it when it is called. For example:
The application M-file contains a "switchyard" that enables it to switch to various execution paths depending on how it is called.
The Switchyard Code
The switchyard functionality in the application M-file is implemented using the feval
function from within an if
statement. feval
evaluates (executes) the subfunction whose name is passed as a string argument when the application M-file is called. feval
executes within a try
/catch
statement to catch errors caused by passing the name of nonexistent subfunctions. The following code generated by GUIDE implements the switchyard (you should not modify this code).
if nargin == 0% If no arguments, open GUI
fig = openfig(mfilename,'reuse');.
.
.
elseif ischar(varargin{1}) % If string argument, call subfunction try [varargout{1:nargout}] = feval(varargin{:}); catch disp(lasterr); end end
Any output arguments returned by your subroutine are then returned though the main function. In addition, all input arguments specified in the Callback
property string are included in the evaluated statement.
See Launching a Dialog to Confirm an Operation for an example that launches the GUI with zero or four arguments.
The following diagram illustrates the execution path for the application M-file.
Adding Input Arguments to Subfunctions
Callback subfunctions added by GUIDE require certain arguments, but have a variable-length argument list. Since the last argument is varargin
, you can add whatever arguments you want to the subfunction. To pass the additional arguments, edit the Callback
property's string to include the arguments. For example, if the string added automatically to the Callback
property is,
my_gui('pushbutton1_Callback',gcbo,[],guidata(gcbo))
change it using the Property Inspector to include the additional arguments. For example,
my_gui('pushbutton1_Callback',gcbo,[],guidata(gcbo),arg1,arg2)
The subfunction, pushbutton1_Callback has the syntax
varargout = pushbutton1_Callback(h,eventdata,handles,varargin)
allowing a variable number of input arguments.
![]() | Automatic Naming of Callback Routines | Initializing the GUI | ![]() |