Skip to Main Content Skip to Search
Product Documentation

Working with Callbacks in GUIDE

Programming GUIs Created Using GUIDE

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.

What Is a Callback?

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.

Kinds of Callbacks

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

ButtonDownFcn

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

Callback

Control action. Executes, for example, when a GUI user clicks a push button or selects a menu item.

Context menu, menu user interface controls

CellEditCallback

Reports any edit made to a value in a table with editable cells; uses event data.

uitable

CellSelectionCallback

Reports indices of cells selected by mouse gesture in a table; uses event data.

uitable

ClickedCallback

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

CloseRequestFcn

Executes when the figure closes.

Figure

CreateFcn

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

DeleteFcn

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

KeyPressFcn

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

OffCallback

Control action. Executes when the State of a toggle tool changes to off.

Toggle tool

OnCallback

Control action. Executes when the State of a toggle tool changes to on.

Toggle tool

ResizeFcn

Executes when a GUI user resizes a panel, button group, or figure whose figure Resize property is On.

Figure, button group, panel

SelectionChangeFcn

Executes when a GUI user selects a different radio button or toggle button in a button group component.

Button group

WindowButtonDownFcn

Executes when you press a mouse button while the pointer is in the figure window.

Figure

WindowButtonMotionFcn

Executes when you move the pointer within the figure window.

Figure

WindowButtonUpFcn

Executes when you release a mouse button.

Figure

WindowKeyPressFcn

Executes when you press a key when the figure or any of its child objects has focus.

Figure

WindowKeyReleaseFcn

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

For additional discussion of how callbacks work and the forms they can take, see What Is a Callback?.

  


Recommended Products

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