Creating Graphical User Interfaces Previous page   Next Page

Introduction to GUI Callbacks

After you have laid out your GUI, you need to program its behavior. The code you write controls how the GUI responds to events such as button clicks, menu item selection, window resizing, or the creation and deletion of components. This programming takes the form of a set of functions, called callbacks, for the GUI figure itself and for each component.

This chapter addresses the programming of GUIs created using GUIDE as well as those created programmatically. This section explains:

What Is a Callback?

A callback is a function that you associate with a specific GUI component or with the GUI figure. It controls component behavior by performing some action in reponse to an event for that component. MATLAB invokes the callback in response to an appropriate event. As an example, suppose a GUI has a button that triggers the plotting of some data. When the user clicks the button, MATLAB calls the callback you associated with clicking that button, and the callback then gets the data and plots it.

A component can be any control device such as a push button or slider. It can also be a menu, a toolbar, a toolbar tool, or a container such as a panel or button group.

Kinds of Callbacks

Each type of component has certain kinds of callbacks with which it can be associated. The possible kinds of callbacks are defined as properties of components. For example, a uipanel has four callback properties: ButtonDownFcn, CreateFcn, DeleteFcn, and ResizeFcn. You can, but are not required to, create a callback function for each of these properties. The GUI itself, which is a figure, also has certain kinds of callbacks with which it can be associated.

Each kind of callback has a triggering mechanism or event that causes it to be called. The following table lists the different kinds of callback properties, the components to which they apply, and the triggering mechanism.

Callback Property
Description
Components
ButtonDownFcn
Executes when the user presses a mouse button while the pointer is on or within five pixels of a component.
Axes,
figure, uibuttongroup, uicontrol, uipanel
Callback
Control action. Executes, for example, when a user clicks a push button or selects a menu item.
Uicontextmenu, uicontrol, uimenu
ClickedCallback
Control action. Executes when the push tool or toggle tool is clicked. For the toggle tool, this is independent of its state.
Uipushtool, uitoggletool
CloseRequestFcn
Executes when the figure closes.
Figure
CreateFcn
Initializes the component when it is created. It executes after the component is created, but before it is displayed.
Axes,
figure, uibuttongroup, uicontextmenu, uicontrol, uimenu, uipanel, uipushtool, uitoggletool, uitoolbar
DeleteFcn
Performs cleanup operations just before the component is destroyed.
Axes,
figure, uibuttongroup, uicontextmenu, uicontrol, uimenu, uipanel, uipushtool, uitoggletool, uitoolbar
KeyPressFcn
Executes when the user presses a keyboard key and the callback's component has focus.
Uicontrol, figure
OffCallback
Control action. Executes when the state of a toggle tool is changed to off.
Uitoggletool
OnCallback
Control action. Executes when the state of a toggle tool is changed to on.
Uitoggletool
ResizeFcn
Executes when a user resizes a panel or button group whose Resize behavior is set to Other.
Figure, uibuttongroup, uipanel
SelectionChangeFcn
Executes when a user selects a different radio button or toggle button in a button group component.
Uibuttongroup
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


Previous page  Working with Callbacks (Draft) GUI Files Next page

© 1994-2005 The MathWorks, Inc.