Documentation

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 namedEventName.hSourceis a handle object that is the source of the event.callbackis a function handle that MATLAB®invokes when the event is triggered.

IfhSourceis 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 Name Event 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

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 inhSource

  • A scalarmeta.propertyobject or an array ofmeta.propertyobjects corresponding to properties defined for the objects inhSource

You can attach listeners to the property events of dynamic properties only whenhSourceis scalar. IfhSourceis non-scalar, then the properties must belong to the class ofhSourceand cannot include dynamic properties (which are not part of the class definition).

The class defining the source property must set theGetObservableandSetObservableproperty 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 anevent.listeneror anevent.proplistenerobject.

Examples

expand all

Create a property listener for theColorproperty of a graphicsfigurewindow.

fig = figure; propListener = listener(fig,'Color','PostSet',@(src,evnt)disp('Color changed'));

设置的值Colorproperty toyellow. Setting the property triggers thePostSetproperty event on the figure. The event source object is the specific figure referenced by the handlefig.

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

Delete the listener object.

delete(propListener)

提示

Listener Lifecycle

To remove a listener, delete the listener object returned bylistener. For example, this statement calls the handle classhandle.deletemethod 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.

Thelistenermethod 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 thelistenermethod 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, usehandle.addlistener.

Alternatives

Useaddlistenerwhen you want MATLABto manage the listener lifecycle.

Introduced in R2017b

Was this topic helpful?