Main Content

event.listener class

Package: event

Class defining listener objects

Description

The event.listener class defines listener objects. Listener objects respond to a specific event by executing a callback function when the event is triggered. You can use the event.listener class to construct a listener object. Also, you can create listeners using the handle addlistener and listener methods.

Use the handle notify method to trigger an event.

The event.listener class is a handle class.

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

example

eListener = event.listener(eventSource,eventName,callbackFcn) creates a listener for the specified event name on the specified source objects and identifies a function handle to the callback function.

If eventSource is an array of object handles, the listener responds to the named event on any of the objects in the array.

Input Arguments

expand all

Event source, specified as a handle object array or a cell array of object handles. Use a cell array when the source objects cannot form an array because their classes differ. All source objects must define the specified event.

Event name, specified as the literal name of the event.

Callback function, specified by a function handle. For more information, see Listener Callback Syntax

Properties

expand all

Event source objects, specified as the handles of the objects that this listener responds to when the event is triggered.

Attributes:

GetAccess
public
SetAccess
public
GetObservable
true
SetObservable
true

Data Types: handle object | cell array

Name of the event that the listener responds to when triggered on the specified source objects.

Attributes:

GetAccess
public
SetAccess
public
GetObservable
true
SetObservable
true

Data Types: char | string

Event callback, specified as a function handle. The function executes when the event is triggered.

Attributes:

GetAccess
public
SetAccess
public
GetObservable
true
SetObservable
true

Data Types: function_handle

If Enabled is set to true (the default), the callback executes when the event occurs. To disable callback execution for this listener, set Enabled to false.

Attributes:

GetAccess
public
SetAccess
public
GetObservable
true
SetObservable
true

Data Types: logical

When false (the default), the listener does not execute its callback recursively. Therefore, if the callback triggers its own event, the listener does not respond again.

When true, the listener callback can cause the same event that triggered the callback. This scheme can lead to infinite recursion, which ends when the MATLAB® recursion limit eventually throws an error.

Attributes:

GetAccess
public
SetAccess
public
GetObservable
true
SetObservable
true

Data Types: logical

Examples

collapse all

Define a listener for an event named EOL with a callback function named EOLCallback that is triggered on an object in the array textReader.

listenerHandle = event.listener(textReader,"EOL",@EOLCallback);

More About

expand all

Version History

Introduced in R2008a