| Contents | Index |
| On this page… |
|---|
After you have laid out your GUI, program its behavior. The code you write controls how the GUI responds to events. Events include button clicks, slider movements, menu item selections, and the creation and deletion of components. This programming takes the form of a set of functions, called callbacks, for each component and for the GUI figure itself.
A callback is a function that you write and associate with a specific GUI component or with the GUI figure. It controls GUI or component behavior by performing some action in response to an event for its component. This programming approach is often called event-driven programming.
When an event occurs for a component, MATLAB software invokes the component's callback that the event triggers. As an example, suppose a GUI has a button that triggers the plotting of some data. When the GUI user clicks the button, the software calls the callback you associated with clicking that button. The callback, which you have programmed, then gets the data and plots it.
A component can be any control device such as a push button, list box, or slider. For purposes of programming, it can also be a menu or a container such as a panel or button group. See Available Components for a list and descriptions of components.
The GUI figure and each type of component can trigger specific kinds of callbacks. The callbacks that are available for each component are properties of that component. For example, a push button has five callback properties: ButtonDownFcn, Callback, CreateFcn, DeleteFcn, and KeyPressFcn. A panel has four callback properties: ButtonDownFcn, CreateFcn, DeleteFcn, and ResizeFcn. You can—but do not have to—create a callback function for each of these properties, including callbacks for the GUI figure itself.
Each callback has a triggering mechanism or event that causes it to execute. The following table lists the callback properties that are available, their triggering events, and the components to which they apply.
Callback Property | Triggering Event | Components |
|---|---|---|
Executes when the GUI user presses a mouse button while the pointer is on or within five pixels of a component or figure. | Axes, figure, button group, panel, user interface controls | |
Control action. Executes, for example, when a GUI user clicks a push button or selects a menu item. | Context menu, menu user interface controls | |
Reports any edit made to a value in a table with editable cells; uses event data. | uitable | |
Reports indices of cells selected by mouse gesture in a table; uses event data. | uitable | |
Control action. Executes when the push tool or toggle tool is clicked. For the toggle tool, executing the callback is state-independent. | Push tool, toggle tool | |
Executes when the figure closes. | Figure | |
Initializes the component when a function creates it. It executes after the component or figure is created, but before it displays. | Axes, button group, context menu, figure, menu, panel, push tool, toggle tool, toolbar, user interface controls | |
Performs cleanup operations just before the component or figure is destroyed. | Axes, button group, context menu, figure, menu, panel, push tool, toggle tool, toolbar, user interface controls | |
Executes when the GUI user presses a keyboard key and the component or figure with this callback has focus. | Figure, user interface controls | |
KeyReleaseFcn | Executes when the GUI user releases a keyboard key and the figure has focus. | Figure |
Control action. Executes when the State of a toggle tool changes to off. | Toggle tool | |
Control action. Executes when the State of a toggle tool changes to on. | Toggle tool | |
Executes when a GUI user resizes a panel, button group, or figure whose figure Resize property is On. | Figure, button group, panel | |
Executes when a GUI user selects a different radio button or toggle button in a button group component. | Button group | |
Executes when you press a mouse button while the pointer is in the figure window. | Figure | |
Executes when you move the pointer within the figure window. | Figure | |
Executes when you release a mouse button. | Figure | |
Executes when you press a key when the figure or any of its child objects has focus. | Figure | |
Executes when you release a key when the figure or any of its child objects has focus. | Figure | |
WindowScrollWheelFcn | Executes when the GUI user scrolls the mouse wheel while the figure has focus. | Figure |
Note User interface controls include push buttons, sliders, radio buttons, check boxes, editable text boxes, static text boxes, list boxes, and toggle buttons. They are sometimes referred to as uicontrols. For details on specific callbacks, follow the links in the preceding table, right-click the property name in the Property Inspector and select the What's this? pop-up menu, or consult the properties reference page for your component, for example, Figure Properties, Uicontrol Properties, Uibuttongroup Properties, or Uitable Properties. |
For additional discussion of how callbacks work and the forms they can take, see What Is a Callback?.
![]() | Programming a GUIDE GUI | Files Generated by GUIDE | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |