| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Events represent changes or actions that occur within class instances. For example,
Modification of class data
Execution of a method
Querying or setting a property value
Destruction of an object
Basically, any activity that you can detected 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 need to respond to the events. The event model works this way:
A handle class declares a name used to represent an event. Naming Events
After creating an instance of the event-declaring class, you can attach listener objects to it. Ways to Create Listeners
A call to a class method broadcasts a notice of the event to listeners. The class user determines when to trigger the event. Triggering Events
Listeners execute a callback function when notified that the event has occurred. Defining Listener Callback Functions
You can bind listeners to the lifecycle of the object that defines the event, or limit listeners to the existence and scope of the listener object. Ways to Create Listeners
The following diagram illustrates the event model.

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:
EventName — The event name as defined in the class event block
Source — The object that is the source of the event
MATLAB passes the source object to the listener callback in the required event data argument. This enables you to access any of the object's public properties from within your listener callback function.
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.
Defining Event-Specific Data provides an example showing how to customize this data.
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 generally useful because the callback cannot access the current state of the object that triggered the event or effect any changes in that object.
Comparing Handle and Value Classes provides general information on handle classes.
Defining Events and Listeners — Syntax and Techniques shows the syntax for defining a handle class and events.
There are four predefined events related to properties:
PreSet — Triggered just before the property value is set, before calling its set access method
PostSet — Triggered just after the property value is set
PreGet — Triggered just before a property value query is serviced, before calling its get access method
PostGet — Triggered just after returning the property value to the query
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:
EventName — The name of the event described by this data object
Source — The source object whose class defines the event described by the data object
AffectedObject — The object whose property is the source for this event (that is, AffectedObject contains the object whose property was either accessed or modified).
You can define your own property-change event data by subclassing the event.EventData class. Note that the event.PropertyEvent class is a sealed subclass of event.EventData.
See Listening for Changes to Property Values for a description of the process for creating property listeners.
See Implementing the PostSet Property Event and Listener for an example.
See Controlling Property Access for information on methods that control access to property values.
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:
Source — Handle or array of handles of the object that generated the event
EventName — Name of the event
Callback — Function to execute with an enabled listener receives event notification
Enabled — Callback function executes only when Enabled is true. See Enabling and Disabling the Listeners for an example.
Recursive — Allow listener to cause the same event that triggered the execution of the callback
Recursive is true by default. It is possible to create a situation where infinite recursion reaches the recursion limit and eventually triggers an error. If you set Recursive to false, the listener cannot execute recursively if the callback triggers its own event.
Ways to Create Listeners provides more specific information.
The order in which listeners execute after the firing of an event is undefined. However, all listener callbacks execute synchronously with the event firing.
![]() | Learning to Use Events and Listeners | Event Attributes | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |