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.
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 the
PropertyRemoved — Triggered when you delete the object or the
meta.DynamicProperty object associated with a dynamic property.
ObjectBeingDestroyed — Triggered when the object is destroyed. This event is inherited from the
These events have public listen access (
ListenAccess attribute) and private notify access (
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 removed
Source — Handle to the object that is the source of the event
EventName — Name of the event (
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 named
SpecialProp is added.
Set the value of the hidden property to
SpecialProp is removed.
event.DynamicPropertyEvent event data to determine the name of the property and whether it is added or deleted.
DynamTest class derives from
dynamicprops. It defines a hidden property,
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
dt = DynamTest;
Add a listener for both
lad = addlistener(dt,'PropertyAdded',@DyPropEvtCb); lrm = addlistener(dt,'PropertyRemoved',@DyPropEvtCb);
PropertyAddedEvent Callback Execution
Adding a dynamic property triggers the
PropertyAdded event. This statement adds a dynamic property to the object and saves the returned
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
ans = 1
PropertyRemovedEvent Callback Execution
Remove a dynamic property by calling
delete on the
meta.DynamicProperty object that is returned by the
addprop method. Removing the
meta.DynamicProperty object triggers the
meta.DynamicProperty object returned when adding the dynamic property
The callback executes:
The value of
HiddenProp is now
ans = 0
You can obtain the
meta.DynamicProperty object for a dynamic property using
findprop if you do not have the object returned by
ad = findprop(dt,'SpecialProp');