MATLAB Creating Graphical User Interfaces

(Barry) #1

8 Programming a GUIDE UI


Callback Property User Action Components That Use This Property
WindowKeyReleaseFcnEnd user releases a key while the
pointer is on the figure or any of
its child objects.

figure

WindowScrollWheelFcnEnd user turns the mouse wheel
while the pointer is on the figure.

figure

GUIDE-Generated Callback Functions and Property Values


How GUIDE Manages Callback Functions and Properties

After you add a uicontrol, uimenu, or uicontextmenu component to your UI, but
before you save it, GUIDE populates the Callback property with the value, %automatic.
This value indicates that GUIDE will generate a name for the callback function.

When you save your UI, GUIDE adds an empty callback function definition to your UI
code file, and it sets the control’s Callback property to be an anonymous function. This
function definition is an example of a GUIDE-generated callback function for a push
button.
function pushbutton1_Callback(hObject,eventdata,handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
end
If you save this UI with the name, myui, then GUIDE sets the push button’s Callback
property to the following value:
@(hObject,eventdata)myui('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
This is an anonymous function that serves as a reference to the function,
pushbutton1_Callback. This anonymous function has four input arguments. The
first argument is a string containing the name of the callback function. The last three
arguments are provided by Handle Graphics®, and are discussed in the section, “GUIDE
Callback Syntax” on page 8-5.

Note:GUIDE does not automatically generate callback functions for other UI
components, such as tables, panels, or button groups. If you want any of these
components to execute a callback function, then you must create the callback by right-
clicking on the component in the layout, and selecting an item under View Callbacks in
the context menu.
Free download pdf