Main Content

listener

Class: handle

Create event listener without binding to event source

Syntax

eL = listener(hSource,EventName,callback)
eL = listener(hSource,PropertyName,EventName,callback)

Description

eL = listener(hSource,EventName,callback) creates a listener for the event named EventName. 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.

If 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.

eL = listener(hSource,PropertyName,EventName,callback) creates a listener for one of the predefined property events. There are four predefined property events:

Event NameEvent Occurs
PreSet

Immediately before the property value is set, before calling its set access method

PostSet

Immediately after the property value is set

PreGet

Immediately before a property value query is serviced, before calling its get access method

PostGet

Immediately after returning the property value to the query

Input Arguments

expand all

Handle object that is the source of the event, specified as a single object or an array of 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.

Data Types: char | string

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 hSource

  • A string or a string array, where each string is the name of a property defined for the objects in hSource

  • A scalar meta.property object or an array of meta.property objects corresponding to properties defined for the objects in hSource

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 GetObservable and SetObservable property attributes to enable you to listen to the property events.

Listener callback specified as a function handle

Data Types: function_handle

Output Arguments

expand all

Listener object, returned as the handle to an event.listener or an event.proplistener object.

Examples

expand all

Create a property listener for the Color property of a graphics figure window.

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 fig.

set(fig,'Color','yellow')

Delete the listener object.

delete(propListener)

Tips

Listener Lifecycle

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.

delete(el)

Calling delete on the listener object destroys the listener and, therefore, the event no longer causes the callback function to execute.

The 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.

Alternatives

Use addlistener when you want MATLAB to manage the listener lifecycle.

Version History

Introduced in R2017b