MATLAB Object-Oriented Programming

(Joyce) #1

  • The order in which listeners execute is not defined.

  • Listeners should not modify the event data object passed to the listener callback,
    because other listeners are passed this same handle object.


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:


  • 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. 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” on page 11-26 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” on page 7-2 provides general information on
handle classes.

“Events and Listeners Syntax” on page 11-22 shows the syntax for defining a handle
class and events.

11 Events — Sending and Responding to Messages

Free download pdf