Main Content


Class: handle

Create event listener bound to event source


el = addlistener(hSource,EventName,callback)
el = addlistener(hSource,PropertyName,EventName,callback)


el = addlistener(hSource,EventName,callback) creates a listener for the event EventName when triggered on the source object, hSource.

If hSource is an array, the listener responds to the named event on any object in the hSource array. callback is a function handle referencing the callback function.

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

addlistener(___) creates a listener object without a handle. You can use this syntax with any of the previous input syntaxes.

Event NameEvent 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

Input Arguments

expand all

Event source is the object that is source of the event, or an array of source objects, specified as a handle array.

Name of 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 your 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 matlab.metadata.Property object or an array of matlab.metadata.Property objects corresponding to properties defined for the objects in hSource

You can attach listeners to property events on 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 created by addlistener, specified as the handle to an listener or an event.proplistener object.


expand all

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

fig = figure;
propListener = addlistener(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.



  • Redefining or clearing the variable containing the handle of the listener (for example, el) does not delete the listener. The event object (hSource) still has a reference to the event.listener object. addlistener ties the listener's lifecycle to the object that is the source of the event.

  • Although the listener is bound to the lifecycle of the event source, the listener continues to exist after the event source is destroyed when:

    • One or more references to the listener are in other variables, including the handle of the listener if one was specified at creation.

    • The listener is tied to other event sources.

  • To remove a listener, delete the listener object returned by addlistener. For example,


    calls the handle class delete method to delete the object from the workspace and remove the listener.

  • To define a listener that is not tied to the event object, use the event.listener constructor directly to create the listener.


When you need the lifecycle of the listener object to be independent of the source object lifecycle, use listener to create listeners.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2008a