Listening for Changes to Property Values

Creating Property Listeners

You can listen to the predeclared property events (named: PreSet, PostSet, PreGet, and PostGet) by creating a listener for those named events:

The following code segments show how to create a PostSet property listener for a property called Limits.

Set Property Attributes to Enable Property Events

In the properties block, set the Limits property SetObservable to true:

properties (SetObservable = true) % Can define property listeners
   Limits = [-2*pi 2*pi];
   ...
end

Define a Callback Function for the Property Event

Define a callback function, passing the required arguments. For example, assume the handlePropertyEvents function is a method of the class whose property is being set. This method requires three arguments:

function handlePropertyEvents(obj,src,evnt)
   switch src.Name % switch on the property name
      case 'Limits'
         obj.calculateData(obj.Limits) % Call class method passing new value for property
   ...
end

The property (obj.Limits) has the new value because this is a PostSet event. Note that src is a meta.property object for the Limits property.

Obtaining Information About Classes with Meta-Classes provides more information about the meta.property class.

Add a Listener to the Property

The addlistener method enables you to attach a listener to a property without storing the listener object as a persistent variable. For a property event, use the four-argument version of addlistener:

lh = addlistener(obj,'Limits','PostSet',@(src,evnt)handlePropertyEvents(obj,src,evnt));
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS