| MATLAB® | ![]() |
| On this page… |
|---|
You can listen to the predeclared property events (named: PreSet, PostSet, PreGet, and PostGet) by creating a listener for those named events:
Set the SetObservable property attribute of the property to true
Define a callback function
Create a property listener by including the name of the property as well as the event in the call to addlistener (see Add a Listener to the Property)
Optionally subclass event.data to create a specialized event data object to pass to the callback function
The following code segments show how to create a PostSet property listener for a property called Limits.
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, 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:
The first argument (obj) is the class instance containing the property that triggers the PostSet event.
The second argument is the event source (the Limits property in this case).
The third argument is the event.PropertyEvent object.
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.
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));
![]() | Defining Events and Listeners — Syntax and Techniques | Example — Using Events to Update Graphs | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |