|On this page…|
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.
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 local function name, from the component Tag property and the name of the callback property.
The following figure shows properties of a push button 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.
Note If you change the string %automatic before saving the GUI, GUIDE does not automatically add a callback for that component or menu item. It is up to you to provide a callback yourself. That callback has to be the same as the string you enter.
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
hObject is the handle of the callback object (in this case, pushbutton1).
eventdata passes a MATLAB struct containing event data. If the object does not generate event data, eventdata contains an empty matrix. The eventdata struct has contents (field names) specific to each type of object that provides it.
When you save the GUI, GUIDE also opens the GUI code file in your editor. The 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 code file, see Adding Callback Templates to an Existing GUI Code File.
The next topic, Customizing Callbacks in GUIDE, provides more information about the callback template.
There are times when you want to delete a callback from a GUI code file. You can delete callbacks whether they are manually or automatically generated. Some common reasons for wanting to delete a callback are:
You delete the component or components to which the callback responded
You want the component to execute different a callback function, which you identify in the appropriate callback property in the Property Inspector. See Changing Callbacks Assigned by GUIDE for instructions and guidelines.
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:
Search for occurrences of the name of the callback in the code.
Open the GUI in GUIDE and use the Property Inspector to check whether any component uses the callback you want to delete.
In either case, if you find a reference to the callback, either remove the reference or retain the callback in the code. Once you have assured yourself that the GUI does not need the code, manually delete the entire callback function from the code file.