|On this page…|
Typically, a GUI code file has the following ordered sections. You can help to maintain the structure by adding comments that name the sections when you first create them.
Initialization tasks such as data creation and any processing that is needed to construct the components. See Initialize a Programmatic GUI for more information.
Initialization tasks that require the components to exist, and output return. See Initialize a Programmatic GUI for more information.
Callbacks for the components. Callbacks are the routines that execute in response to user-generated events such as mouse clicks and key strokes. See Write Callbacks Using the Programmatic Workflow for more information.
This is a template you can use to create a GUI code file:
function varargout = mygui(varargin) % MYGUI Brief description of GUI. % Comments displayed at the command line in response % to the help command. % (Leave a blank line following the help.) % Initialization tasks % Construct the components % Initialization tasks % Callbacks for MYGUI % Utility functions for MYGUI end
The end statement that matches the function statement is necessary because this document treats GUI creation using nested functions.
Save the file in your current folder or at a location that is on your MATLAB path.
You can display your GUI at any time by executing its code file. For example, if your GUI code file is mygui.m, type
at the command line. Provide run-time arguments as appropriate. The file must reside on your path or in your current folder.
When you execute the code, a fully functional copy of the GUI displays on the screen. If the file includes code to initialize the GUI and callbacks to service the components, you can manipulate components that it contains.