Dynamic Property Events
Dynamic Properties and Ordinary Property Events
Dynamic properties support property set and get events so you can define listeners for these properties. Listeners are bound to the particular dynamic property for which they are defined.
If you delete a dynamic property, and then create another dynamic property with the same name, the listeners do not respond to events generated by the new property. A listener defined for a dynamic property that has been deleted does not cause an error, but the listener callback is never executed.
Property-Set and Query Events provides more information on how to define listeners for these events.
Dynamic-Property Events
To respond to the addition and removal of dynamic properties, attach listeners to objects containing the dynamic properties. The dynamicprops
class defines events for this purpose:
PropertyAdded
— Triggered when you add a dynamic property to an object derived from thedynamicprops
class.PropertyRemoved
— Triggered when you delete the object or thematlab.metadata.DynamicProperty
object associated with a dynamic property.ObjectBeingDestroyed
— Triggered when the object is destroyed. This event is inherited from thehandle
class.
These events have public listen access (ListenAccess
attribute) and private notify access (NotifyAccess
attribute).
The PropertyAdded
and PropertyRemoved
events pass an event.DynamicPropertyEvent
object to listener callbacks. The event data object has three properties:
PropertyName
— Name of the dynamic property that is added or removedSource
— Handle to the object that is the source of the eventEventName
— Name of the event (PropertyAdded
,PropertyRemoved
, orObjectBeingDestroyed
)
Listen for a Specific Property Name
Suppose that you have an application that creates a dynamic property under certain conditions. You want to:
Set the value of a hidden property to
true
when a property namedSpecialProp
is added.Set the value of the hidden property to
false
whenSpecialProp
is removed.
Use the event.DynamicPropertyEvent
event data to determine the name of the property and whether it is added or deleted.
The DynamTest
class derives from dynamicprops
. It defines a hidden property, HiddenProp
.
classdef DynamTest < dynamicprops properties (Hidden) HiddenProp end end
Define a callback function that uses the EventName
property of the event data to determine if a property is added or removed. Obtain the name of the property from the PropertyName
property of the event data. If a dynamic property is named SpecialProp
, change the value of the hidden property.
function DyPropEvtCb(src,evt) switch evt.EventName case 'PropertyAdded' switch evt.PropertyName case 'SpecialProp' % Take action based on the addition of this property %... %... src.HiddenProp = true; disp('SpecialProp added') otherwise % Other property added % ... disp([evt.PropertyName,' added']) end case 'PropertyRemoved' switch evt.PropertyName case 'SpecialProp' % Take action based on the removal of this property %... %... src.HiddenProp = false; disp('SpecialProp removed') otherwise % Other property removed % ... disp([evt.PropertyName,' removed']) end end end
Create an object of the DynamTest
class.
dt = DynamTest;
Add a listener for both PropertyAdded
and PropertyRemoved
events.
lad = addlistener(dt,'PropertyAdded',@DyPropEvtCb); lrm = addlistener(dt,'PropertyRemoved',@DyPropEvtCb);
PropertyAdded
Event Callback Execution
Adding a dynamic property triggers the PropertyAdded
event. This
statement adds a dynamic property to the object and saves the returned
matlab.metadata.DynamicProperty
object.
ad = addprop(dt,'SpecialProp');
The addition of the dynamic property causes the listener to execute its callback function, DyPropEvtCb
. The callback function assigns a value of true
to the HiddenProp
property.
dt.HiddenProp
ans = 1
PropertyRemoved
Event Callback Execution
Remove a dynamic property by calling delete
on the matlab.metadata.DynamicProperty
object
that is returned by the addprop
method. Removing the
matlab.metadata.DynamicProperty
object triggers the
PropertyRemoved
event.
Delete the matlab.metadata.DynamicProperty
object returned when
adding the dynamic property SpecialProp
.
delete(ad)
The callback executes:
SpecialProp removed
The value of HiddenProp
is now false
.
dt.HiddenProp
ans = 0
How to Find matlab.metadata.DynamicProperty
Objects
You can obtain the matlab.metadata.DynamicProperty
object for a
dynamic property using findprop
. Use
findprop
if you do not have the object returned by
addprop
.
ad = findprop(dt,'SpecialProp');