MATLAB Object-Oriented Programming

(Joyce) #1

function callbackFunction(src,evnt)
...
end


If you do not use the event source and event data arguments, you can define the function
to ignore these inputs:


function callbackFunction(~,~)
...
end


For a method:


function callbackMethod(obj,src,evnt)
...
end


Additional Arguments for Callback Function


To pass arguments to your callback in addition to the source and event data arguments
passed by MATLAB, use an anonymous function. Anonymous functions can use any
variables that are available in the current workspace.


Syntax Using Anonymous Function


Here is the syntax for an ordinary method. The input arguments (arg1,...argn) must
be defined in the context in which you call addlistener.


lh = addlistener(src,'EventName',@(src,evnt)obj.callbackMethod(src,evnt,arg1,...argn)


Use varargin to define the callback function.


function callbackMethod(src,evnt,varargin)
arg1 = varargin{1};
...
argn = varargin{n};
...
end


For general information on anonymous function, see “Anonymous Functions”.


Listener Callback Syntax 11-

Free download pdf