Specifications
4 Programming GUIs
4-2
Understanding the GUI M-File
The GUI M-file generated by GUIDE controls the GUI and determines how it
responds to a user’s action, such as pressing a push button or selecting a menu
item. The M-file contains all the code needed to run the GUI, including the
callbacks for the GUI components. While GUIDE generates the framework for
this M-file, you must program the callbacks, which are subfunctions of the
M-file, to perform the functions you want them to.
This section explains the overall structure of the M-file. The section covers the
following topics:
• “Sharing Data with the Handles Structure” on page 4-2
• “Functions and Callbacks in the M-File” on page 4-3
• “Opening Function” on page 4-4
• “Output Function” on page 4-5
• “Callbacks” on page 4-6
• “Input and Output Arguments” on page 4-7
To learn more about programming callbacks, see Subfunctions in the online
MATLAB documentation.
Sharing Data with the Handles Structure
When you run a GUI, the M-file creates a handles structure that contains all
the data for GUI objects, such as controls, menus, and axes. The
handles
structure is passed as an input to each callback. You can use the
handles
structure to
• Share data between callbacks
• Access GUI data
Sharing Data
To store data that is contained in a variable X, set a field of the handles
structure equal to
X and then save the handles structure with the guidata
function:
handles.current_data = X;
guidata(hObject,handles)