Listeners execute their callback function when notified that the event has occurred. Listeners are passive observers in the sense that errors in the execution of a listener callback do not prevent the execution of other listeners responding to the same event, or execution of the function that triggered the event.
Callback function execution continues until the function completes. If an error occurs in a callback function, execution stops and control returns to the calling function. Then any remaining listener callback functions execute.
The order in which listeners callback functions execute after the firing of an event is undefined. However, all listener callbacks execute synchronously with the event firing.
The handle class
notify method calls all listeners before returning execution to the function that called
Do not modify and reuse or copy and reuse the event data object that you pass to
notify, which is then passed to the listener callback.
Listener callbacks can call
notify to trigger events, including the same event that invoked the callback. When a function calls
notify, MATLAB® sets the property values of the event data object that is passed to callback functions. To ensure that these properties have appropriate values for subsequently called callbacks, always create a new event data object if you call
notify with custom event data.
If you want to control how your program responds to errors, use a
try/catch statement in your listener callback function to handle errors.
When you create a function handle inside a class method, the context of the method determines the context in which the function executes. This context gives the function access to private and protected methods that are accessible to that class.
For example, the
UpdateEvt class defines an event named
Update and a listener for that event. The listener callback is the private method
classdef UpdateEvt < handle events Update end methods function obj = UpdateEvt addlistener(obj,'Update',@evtCb); end end methods (Access = private) function obj = evtCb(obj,varargin) disp('Updated Event Triggered') end end end
Private methods are normally accessible only by class methods. However, because the function handle is created in a class method,
notify can execute the callback from outside of the class:
a = UpdateEvt; a.notify('Update')
Updated Event Triggered