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:
Only
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
event.EventDataclass.
Predefined Events
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.
Event Syntax
Define an event name in the events code block:
classdef ClassName < handle
   events
      EventName
   end
endFor example, MyClass defines an event named StateChange:
classdef MyClass < handle events StateChange end end
Trigger an event using the handle class notify method:
classdef ClassName < handle
   events
      EventName
   end
   methods
      function anyMethod(obj)
         notify(obj,'EventName');
      end
   end
endAny 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 StateChange event:
classdef MyClass < handle events StateChange end methods function triggerEvent(obj) notify(obj,'StateChange') end end end
Trigger the StateChange event with the triggerEvent method:
obj = MyClass; obj.triggerEvent
For more information, see Events and Listeners Syntax.
Create Listener
Define a listener using the handle class addlistener or listener
            method. Pass a function handle for the listener callback function using one of these
            syntaxes:
addlistener(SourceOfEvent,'— for an ordinary function.EventName',@functionName)addlistener(SourceOfEvent,'— for a method ofEventName',@Obj.methodName).Objaddlistener(SourceOfEvent,'— for a static method of the classEventName',@ClassName.methodName).ClassName
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.SourceOfEvent— The name of the event defined in the classEventNameeventscode block.@— a function handle referencing the function that executes in response to the event.listenerCallback
For example, create a listener object for the StateChange event:
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.