Event and Listener Concepts - MATLAB & Simulink (original) (raw)

The Event Model

Events represent changes or actions that occur within objects. For example,

Basically, any activity that you can detect programmatically can generate an event and communicate information to other objects.

MATLAB® classes define a process that communicates the occurrence of events to other objects that respond to the events. The event model works this way:

This diagram illustrates the event model.

Model of events and listeners

Limitations

There are certain limitations to the use of events:

Default Event Data

Events provide information to listener callbacks by passing an event data argument to the callback function. By default, MATLAB passes an event.EventData object to the listener callback. This object has two properties:

MATLAB passes the source object to the listener callback in the required event data argument. Use the source object to access any of the object's public properties from within your listener callback function.

Customize Event Data

You can create a subclass of the event.EventData class to provide additional information to listener callback functions. The subclass would define properties to contain the additional data and provide a method to construct the derived event data object so it can be passed to the notify method.

Define Event-Specific Data provides an example showing how to customize this data.

Events Only in Handle Classes

You can define events only in handle classes. This restriction exists because a value class is visible only in a single MATLAB workspace so no callback or listener can have access to the object that triggered the event. The callback could have access to a copy of the object. However, accessing a copy is not useful because the callback cannot access the current state of the object that triggered the event or effect any changes in that object.

Comparison of Handle and Value Classes provides general information on handle classes.

Events and Listeners Syntax shows the syntax for defining a handle class and events.

Property-Set and Query Events

There are four predefined events related to properties:

These events are predefined and do not need to be listed in the class events block.

When a property event occurs, the callback is passed an event.PropertyEvent object. This object has three properties:

You can define your own property-change event data by subclassing the event.EventData class. The event.PropertyEvent class is a sealed subclass of event.EventData.

See Listen for Changes to Property Values for a description of the process for creating property listeners.

See The PostSet Event Listener for an example.

See Property Get and Set Methods for information on methods that control access to property values.

Listeners

Listeners encapsulate the response to an event. Listener objects belong to the event.listener class, which is a handle class that defines the following properties:

Control Listener Lifecycle provides more specific information.