Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Associating Callbacks with Components

GUI Components

A GUI can have many components. GUIDE provides a way of specifying which callback runs in response to a particular event for a particular component. The callback that runs when the GUI user clicks a Yes button is not the one that runs for the No button. Similarly, each menu item usually performs a different function. See Kinds of Callbacks for a list of callback properties and the components to which each applies.

Setting Callback Properties Automatically

GUIDE initially sets the value of the most commonly used callback properties for each component to %automatic. For example, a push button has five callback properties, ButtonDownFcn, Callback, CreateFcn, DeleteFcn, and KeyPressFcn. GUIDE sets only the Callback property, the most commonly used callback, to %automatic. You can use the Property Inspector to set the other callback properties to %automatic. To do so, click the pencil-and-paper icon next to the callback name. GUIDE immediately replaces %automatic with a MATLAB expression that is the GUI calling sequence for the callback. Within the calling sequence, it constructs the callback name, for example, the subfunction name, from the component Tag property and the name of the callback property.

The following figure shows a push button's properties in the GUIDE Property Inspector prior to saving the GUI. GUIDE set the Tag property to pushbutton1. Before saving the GUI, Callback property displays as %automatic, indicating that GUIDE will generate a name for it when you save the GUI.

When you save the GUI, GUIDE constructs the name of the callback by appending an underscore (_) and the name of the callback property to the value of the component's Tag property. For example, the MATLAB expression for the Callback property for a push button in the GUI untitled with Tag property pushbutton1 is

untitled('pushbutton1_Callback',hObject,eventdata,guidata(hObject))

In this case, untitled is the name of the GUI M-file as well as the name of the main function for that GUI. The remaining arguments generate input arguments for pushbutton1_Callback. Specifically,

See Input Arguments and Callback Function Signatures for more details about callback arguments and how to customize them.

When you save the GUI, GUIDE also opens the GUI M-file in your editor. The M-file then contains a template for the Callback callback for the component whose Tag is pushbutton1. If you activate the GUI, clicking the push button triggers the execution of the Callback callback for the component.

For information about changing the callback name after GUIDE assigns it, see Changing Callbacks Assigned by GUIDE. For information about adding callback templates to the GUI M-file, see Adding Callback Templates to an Existing GUI M-File.

The next topic, Callback Syntax and Arguments, provides more information about the callback template.

Deleting Callbacks from a GUI M-File

There are times when you want to delete a callback from a GUI M-file. You can delete callbacks whether they are manually or automatically generated. Some common reasons for wanting to delete a callback are:

Only delete a callback if you are sure that the callback is not used. To ensure that the callback is not used elsewhere in the GUI:

In either case, if you find a reference to the callback, either remove the reference or retain the callback in the GUI M-file. Once you have assured yourself that the GUI does not need the code, manually delete the entire callback function from the M-file.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS