Create event listener without binding to event source
eL = listener(hSource,EventName,callback)
eL = listener(hSource,PropertyName,EventName,callback)
creates a listener for the event named
eL = listener(
hSource is a handle object that is the source of the event.
callback is a function handle that MATLAB® invokes when the event is triggered.
hSource is an array of event source objects, the listener responds to the named event on any object in the array that is not in a deleted state.
|Event Name||Event Occurs|
Immediately before the property value is set, before calling its set access method
Immediately after the property value is set
Immediately before a property value query is serviced, before calling its get access method
Immediately after returning the property value to the query
hSource— Handle object that is the source of the event
Handle object that is the source of the event, specified as a single object or an array of objects.
EventName— Name of the event that is triggered on the source objects
Name of the event that is triggered on the source objects, specified as case-sensitive, quoted text. For property events, the event name is one of the four predefined property events.
PropertyName— Name of property
Name of the property whose property event triggers the listener, specified as one of these values:
A character vector or a cell array of character vectors, where each character vector is the name of a property defined for the objects in
A string or a string array, where each string is the name of a
property defined for the objects in
meta.property object or an array of
meta.property objects corresponding to properties defined for the objects in
You can attach listeners to the property events of dynamic properties only when
hSource is scalar. If
hSource is non-scalar, then the properties must belong to the class of
hSource and cannot include dynamic properties (which are not part of the class definition).
The class defining the source property must set the
SetObservable property attributes to enable you to listen to the property events.
callback— Listener callback
Listener callback specified as a function handle
el— Listener object
Listener object, returned as the handle to an
event.listener or an
Create a property listener for the
Color property of a graphics
fig = figure; propListener = listener(fig,'Color','PostSet',@(src,evnt)disp('Color changed'));
Set the value of the
Color property to
yellow. Setting the property triggers the
PostSet property event on the figure. The event source object is the specific figure referenced by the handle
Delete the listener object.
To remove a listener, delete the listener object returned by
listener. For example, this statement calls the handle class
delete method to remove the listener.
Calling delete on the listener object destroys the listener and, therefore, the event no longer causes the callback function to execute.
listener method does not bind the listener's lifecycle to the object that is the source of the event. Destroying the event source object does not affect the lifecycle of the listener object.
You must explicitly destroy listeners created with the
listener method independently of the source object. Calling the handle delete method on the listener variable (for example,
delete(el)) explicitly destroys the listener. Redefining or clearing the variable containing the listener can delete the listener if there are no other references to it. To bind the lifecycle of the listener to the lifecycle of the event source object, use
addlistener when you want MATLAB to manage the listener lifecycle.