MATLAB Object-Oriented Programming

(Joyce) #1

Listen for Changes to Property Values


In this section...
“Create Property Listeners” on page 11-40
“Property Event and Listener Classes” on page 11-42

Create Property Listeners


For handle classes, you can define listeners for the predeclared property events (named:
PreSet, PostSet, PreGet, and PostGet). To create listeners for those named events:


  • Specify the SetObservable and/or GetObservable property attributes.

  • Define callback functions

  • Create the property listener by including the name of the property and the event in
    the call to handle.addlisteneror handle.listener.

  • If necessary, subclass event.data to create a specialized event data object to pass to
    the callback function.

  • Prevent execution of the callback if the new value is the same as the current value
    (see “Assignment When Property Value Is Unchanged” on page 11-44).


Set Property Attributes to Enable Property Events

In the properties block, enable the SetObservable attribute. You can define PreSet and
PostSet listeners for the properties defined in this block:

properties (SetObservable)
PropOne
PropTwo
...
end

Define Callback Function for Property Event

The listener executes the callback function when MATLAB triggers the property event.
Define the callback function to have two specific arguments, which are passed to the
function automatically when called by the listener:


  • Event source — a meta.property object describing the object that is the source of
    the property event


11 Events — Sending and Responding to Messages

Free download pdf