MATLAB Examples

Events and listeners

To monitor an object you first need to add an event to the class definition of the object, such as in this example. (see ev.m). Here we add an event called SavedData to this class which denotes when the class saves some data to disk. It could then be accessed by someone else.

It is the responsibility of the class to fire the event whenever it wants by calling the notify function in a method such as in the updateData method.


Monitoring an object

To monitor a object's events, you need to...

  • Create a call back function {see evcallback} what you would like executed when the event is fired. Here it just displays some text
  • Create the object to monitor
  • Add a listener to the object specifying this callback
  • When the method that includes the notify call is executed, we see the call back is called
Data was saved

There are also special events that let you monitor things such as property changes for example. The property has to specify the SetObservable attribute.

Following code need SetObservable to be added

Note that, if you modify and save a classdef file, it will not be used if there are existing workspace objects based on or inheriting from that definition file. You will get a warning. I will try to use the new object.


When this happens, use the clear command to remove those variables.

%a=ev % I will create again

Now we will add a listener to execute a callback whenever the data property of the object changes {step, step}


When the property's value changes the call back is executed;