Graphics Programs Reference
In-Depth Information
GUI Callback Functions
When you are ready to create a callback function for a given object, click
the right mouse button on the object and select Edit Callback . The M-file
associated with the GUI will be brought to the front in an Editor/Debugger
window, with the cursor in a block of lines like the ones below. (If you haven't
yet saved the GUI, you will be prompted to do so first, so that GUIDE knows
what name to give the M-file.)
function varargout = pushbutton1 Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.pushbutton1.
disp('pushbutton1 Callback not implemented yet.')
% ------------------- end pushbutton1 Callback -----------------------
In this case we have assumed that the object you selected was the first Push
Button that you created in the Layout Editor; the string “pushbutton1” above
is its default tag . (Another way to find the tag for a given object is to select
it and look next to “Tag” in the Property Inspector.) All you need to do now
to bring this Push Button to life is to replace the disp command line in the
template shown above with the commands that you want performed when the
user clicks on the button. Of course you also need to save the M-file, which you
can do in the usual way from the Editor/Debugger, or by activating the GUI
from the Layout Editor. Each time you save or activate a GUI, a block of four
lines like the ones above is automatically added to the GUI's M-file for any
new objects or menu items that you have added to the GUI and that should
have callback functions.
In the example plotgui from the previous section, there is one case where
we used an existing MATLAB command as a callback function. For the Push
Button labeled “Change axis limits”, we simply entered axlimdlg into its
callbackfunctionin lo i. .Thiscommandopensa dialogbox thatallows
a user to type new values for the ranges of the x and y axes. MATLAB has a
number of dialog boxes that you can use either as callback functions or in an
ordinary M-file. For example, you can use inputdlg in place of input . Type
help uitools for information on the available dialog boxes.
For the Popup Menu on the right side of the GUI, we put the following lines
into its callback function template:
switch get(h, 'Value')
case 1
axis equal
case 2
axis normal
Search WWH ::




Custom Search