Skip to Main Content Skip to Search
Product Documentation

addlistener (handle) - Create event listener

Syntax

lh = addlistener(Hsource,'EventName',callback)
lh = addlistener(Hsource,property,'EventName',callback)

Description

lh = addlistener(Hsource,'EventName',callback) creates a listener for the specified event.

lh = addlistener(Hsource,property,'EventName',callback) creates a listener for one of the predefined property events. There are four property events:

See Events and Listeners — Syntax and Techniques for more information.

Input Arguments

Hsource

Handle of the object that is the source of the event, or an array of source handles.

EventName

Name of the event, which is triggered by the source objects.

callback

Function handle referencing a function to execute when the event is triggered.

property

Character string that can be:

  • the name of the property

  • a cell array of strings where each string is the name of a property that exists in object array Hsource

  • a meta.property object or an array of meta.property objects

  • a cell array of meta.property objects

If Hsource is a scalar, then any of the properties can be dynamic properties. If Hsource is non-scalar, then the properties must belong to the class of Hsource and can not include dynamic properties (which are not part of the class definition).

For more information, see the following sections:

Output Arguments

lh

Handle of the event.listener object returned by addlistener.

Removing a Listener

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

delete(lh)

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

When you use addlistener to create a listener, redefining the variable containing the handle to the listener does not delete the listener because the event object 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. If you want to define a listener that is not tied to the event object, use the event.listener constructor directly to create the listener.

See Also

delete (handle) | handle | notify (handle)

How To

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS