Overview Events and Listeners
Why Use Events and Listeners
Events are notices that objects broadcast in response to something that happens, such as a property value changing or a user interaction with an application program. Listeners execute functions when notified that the event of interest occurs. Use events to communicate changes to objects. Listeners respond by executing the callback function.
For more information, see Event and Listener Concepts.
Events and Listeners Basics
When using events and listeners:
handleclasses can define events and listeners.
Define event names in the
eventsblock of a class definition (Events and Listeners Syntax).
Use event attributes to specify access to the event (Event Attributes).
Call the handle
notifymethod to trigger the event. The event notification broadcasts the named event to all listeners registered for this event.
Use the handle
addlistenermethod to couple a listener to the event source object. MATLAB® destroys the listener when the source of the event is destroyed.
Use the handle
listenermethod to create listeners that are not coupled to the lifecycle of the event source object. This approach is useful when the event source and the listeners are defined in different components that you want to be able to add, remove, or modify independently. Your application code controls the listener object lifecycle.
Listener callback functions must define at least two input arguments — the event source object handle and the event data (See Listener Callback Syntax for more information).
Modify the data passed to each listener callback by subclassing the
MATLAB defines events for listening to property sets and queries. For more information, see Listen for Changes to Property Values.
All handle objects define an event named
ObjectBeingDestroyed. MATLAB triggers this event before calling the class destructor.
Define an event name in the
events code block:
classdef ClassName < handle events EventName end end
MyClass defines an event named
classdef MyClass < handle events StateChange end end
classdef ClassName < handle events EventName end methods function anyMethod(obj) notify(obj,'EventName'); end end end
Any function or method can trigger the event for a specific instance of the class defining the event. For example, the
triggerEvent method calls
notify to trigger the
classdef MyClass < handle events StateChange end methods function triggerEvent(obj) notify(obj,'StateChange') end end end
StateChange event with the
obj = MyClass; obj.triggerEvent
For more information, see Events and Listeners Syntax.
addlistener(SourceOfEvent,'— for an ordinary function.
addlistener(SourceOfEvent,'— for a method of
addlistener(SourceOfEvent,'— for a static method of the class
ListenerObject = addlistener(SourceOfEvent,'EventName',@listenerCallback);
addlistener returns the listener object. The input arguments are:
— An object of the class that defines the event. The event is triggered on this object.
— The name of the event defined in the class
@— a function handle referencing the function that executes in response to the event.
For example, create a listener object for the
function lh = createListener(src) lh = addlistener(src,'StateChange',@handleStateChange) end
Define the callback function for the listener. The callback function must accept as the first two arguments the event source object and an event data object: Use the event source argument to access the object that triggered the event. Find information about the event using the event data object.
function handleStateChange(src,eventData) % src - handle to object that triggered the event % eventData - event.EventData object containing % information about the event. ... end
For more information, see Listener Callback Syntax.