MATLAB Creating Graphical User Interfaces

(ff) #1
b = uicontrol('Style','pushbutton','Callback',@pushbutton_callback);

Here is the function definition for pushbutton_callback:

function pushbutton_callback(src,event)
display('Button pressed');
end

Notice that the function handle does not explicitly refer to any input arguments, but the
function declaration includes two input arguments. These two input arguments are
required for all callbacks you specify as a function handle. MATLAB passes these
arguments automatically when the callback executes. The first argument is the UI
component that triggered the callback. The second argument provides event data to the
callback function. If there is no event data available to the callback function, then
MATLAB passes the second input argument as an empty array. The following table lists
the callbacks and components that use event data.

Callback Property Name Component
WindowKeyPressFcn
WindowKeyReleaseFcn
WindowScrollWheel

figure

KeyPressFcn figure, uicontrol, uitable
KeyReleaseFcn figure, uicontrol, uitable
SelectionChangedFcn uibuttongroup
CellEditCallback
CellSelectionCallback

uitable

A benefit of specifying callbacks as function handles is that MATLAB checks the function
for syntax errors and missing dependencies when you assign the callback to the
component. If there is a problem in the callback function, then MATLAB returns an error
immediately instead of waiting for the user to trigger the callback. This behavior helps
you to find problems in your code before the user encounters them.

Specify a Cell Array

Use a cell array to specify a callback function that accepts additional input arguments
that you want to use in the function. The first element in the cell array is a function
handle. The other elements in the cell array are the additional input arguments you want
to use, separated by commas. The function you specify must define the same two input

10 Code a Programmatic App

Free download pdf