MATLAB Object-Oriented Programming

(Joyce) #1
If your listener callback is an ordinary method and not a static method, the syntax is:
addlistener(EventObject,'PropOne','PostSet',@obj.handlePropertyEvents);

where obj is the handle of the object defining the callback method.

If the listener callback is a function that is not a class method, you pass a function handle
to that function. Suppose that the callback function is a package function:
addlistener(EventObject,'PropOne','PostSet',@package.handlePropertyEvents);

For more information on passing functions as arguments, see “Create Function Handle”.

Property Event and Listener Classes


The following two classes show how to create PostSet property listeners for two
properties — PropOne and PropTwo.

Class Generating the Event

The PropEvent class enables property PreSet and PostSet event triggering by
specifying the SetObservable property attribute. These properties also enable the
AbortSet attribute, which prevents the triggering of the property events if the
properties are set to a value that is the same as their current value (see “Assignment
When Property Value Is Unchanged” on page 11-44).

classdef PropEvent < handle
properties (SetObservable, AbortSet)
PropOne
PropTwo
end
methods
function obj = PropEvent(p1,p2)
if nargin > 0
obj.PropOne = p1;
obj.PropTwo = p2;
end
end
end
end

Class Defining the Listeners

The PropListener class defines two listeners:

11 Events — Sending and Responding to Messages

Free download pdf