MATLAB Object-Oriented Programming

(Joyce) #1
Using Methods for Callbacks

The TestAnonyFcn class shows the use of an anonymous function with an additional
argument. The listener callback displays the inputs arguments to show how MATLAB calls
the callback method.

classdef TestAnonyFcn < handle
events
Update
end
methods
function obj = TestAnonyFcn
t = datestr(now);
addlistener(obj,'Update',@(src,evnt)obj.evntCb(src,evnt,t));
end
function triggerEvnt(obj)
notify(obj,'Update')
end
end
methods (Access = private)
function evntCb(~,~,evnt,varargin)
disp(['Number of inputs: ',num2str(nargin)])
disp(evnt.EventName)
disp(varargin{:})
end
end
end

Create an object and trigger the event by calling the triggerEvt method:

obj = TestAnonyFcn;
obj.triggerEvnt;

Number of inputs: 4
Update
01-Jul-2008 17:19:36

See Also


Related Examples



  • “Callback Execution” on page 11-34


11 Events — Sending and Responding to Messages

Free download pdf