MATLAB Object-Oriented Programming

(Joyce) #1
The PostSet event does not occur until an actual assignment of the property occurs. The
property set function provides an opportunity to deal with potential assignment errors
before the PostSet event occurs.

Enable PostSet Property Event

To create a listener for the PostSet event, you must set the property's SetObservable
attribute to true:

properties (SetObservable = true)
Lm = [-2*pi 2*pi]; % specifies default value
end

MATLAB automatically triggers the event so it is not necessary to call notify.

“Specify Property Attributes” on page 8-7 provides a list of all property attributes.

Listener and Callback for PostSet Event

The fcnview class creates a listener for the PostSet event using the addlistener
method:
obj.HLLm = addlistener(obj.FcnObject,'Lm','PostSet',...
@(src,evnt)listenLm(obj,src,evnt)); % Add obj to argument list

The fcnview object stores a handle to the event.listener object in its HLLm property,
which is used to enable/disable the listener by a context menu (see “Enable and Disable
Listeners” on page 11-63).

The fcnview object (obj) is added to the two default arguments (src, evnt) passed to
the listener callback. Keep in mind, the source of the event (src) is the fcneval object,
but the fcnview object contains the handle of the surface object that the callback
updates.

The callback sets the axes limits and updates the surface data because changing the
limits causes the mathematical function to be evaluated over a different range:

function listenLm(obj,src,evnt)
if ishandle(obj.HAxes) % If there is an axes
lims(obj); % Update its limits
if ishandle(obj.HSurface) % If there is a surface
obj.updateSurfaceData % Update its data
end
end
end

11 Events — Sending and Responding to Messages

Free download pdf