User`s guide

WriteCodeforCallbacks
Use Cell Arrays with Function Handles. You can specify a callback
function using a function handle instead of using a function name.Themajor
benefit to using function handles is the capability to define functions on the
fly—by executing code that sets a component’s callback to the handle of
a function defined within its scope, for example, an anonym ous function.
Dynamic callback assignment enables callbacks to change their behavior
according to the context within which they operate or data they process.
Never enclose function handles in quotes when you declare callbacks.
The following variation uses a function handle to specify
pushbutton_callback as the callback routine to be executed when a user
clicks Plot line.
figure;
hb = uicontrol('Style','pushbut ton',...
'String','Plot line')
set(hb,'Callback',{@pushbutton_call back,myvar,'--m'})
Callback
isthenameofthecallbackproperty. Thefirstelementofthecell
array is the handle of the callback routine, and subsequent elements are input
arguments to the callback. Function handles are not strings or filenames,
so do not place single quote marks around them. Only use quote marks for
callback arguments that are literal strings, such the
linespec '--m' in the
above example. The second a nd third elements of the cell array, the variable
myvar and the string '--m', become the third and fourth argument of the
callback, after
hObject and eventdata.
As above, the callback is in a file nam ed
pushbutton_callback.m,which
contains code such as this:
function pushbu tton _callback(hObject, eventdata, var1, va r2)
plot(var1,var2)
As you can see from the previous examples, y ou can specify either a function
name (enclosed in single quote marks) or a function handle (without single
quote marks) in a callback using a cell array and achieve the same results.
Using function handles gives you additional flexibility when your application
needs to behave dynamically.
12-17