Dynamic Property Events - MATLAB & Simulink (original) (raw)

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:

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:

Listen for a Specific Property Name

Suppose that you have an application that creates a dynamic property under certain conditions. You want to:

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.

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 returnedmatlab.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.

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 thematlab.metadata.DynamicProperty object triggers thePropertyRemoved event.

Delete the matlab.metadata.DynamicProperty object returned when adding the dynamic property SpecialProp.

The callback executes:

The value of HiddenProp is now false.

How to Find matlab.metadata.DynamicProperty Objects

You can obtain the matlab.metadata.DynamicProperty object for a dynamic property using findprop. Usefindprop if you do not have the object returned byaddprop.

ad = findprop(dt,'SpecialProp');

See Also

Topics