Specifications
Understanding the Application M-File
4-3
Understanding the Application M-File
MATLAB generates the application M-file to provide a framework for the
program that controls the GUI. This framework fosters a programming style
that is efficient and robust. All code, including the callbacks, is contained in the
single application M-file.
The following code is the initialization section of the application M-file. This
code is generated by GUIDE.
function varargout = my_gui(varargin)
if nargin == 0 % If no input arguments, launch the GUI
fig = openfig(mfilename,'reuse');
% Use system color scheme for figure:
set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));
% Generate a structure of handles to pass to callbacks, and store it.
handles = guihandles(fig);
guidata(fig, handles);
% ------------------------------------------
% Add any necessary initialization code here
% -------------------------------------------
if nargout > 0
varargout{1} = fig;
end
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
try
if (nargout)
[varargout{1:nargout}] = feval(varargin{:});
else
feval(varargin{:}); % FEVAL switchyard
end
catch
disp(lasterr);
end
end
Whether you use the GUIDE-generated application M-file or create your own
code, the programming techniques discussed here provide useful approaches to
GUI programming. The following sections discuss the architecture and
functioning of the application M-file: