Dynamic Properties — Adding Properties to an Instance

What Are Dynamic Properties

Use dynamic properties to associated data with instances of classes without modifying the class definition. This is useful for attaching temporary data to objects. You can access dynamic properties like any class-defined properties to set and query their values. You can also set property attributes, add property set and get methods, and define listeners to respond to property change events.

It is possible for more than one program to define dynamic properties on the same object so you should take care to avoid name conflicts.

Defining Dynamic Properties

Any class that is a subclass of the dynamicprops class (which is itself a subclass of the handle class) can define dynamic properties using the addprop method. The syntax is:

P = addprop(H,'PropertyName')

where:

P is an array of meta.Dynamicproperty objects

H is an array of handles

PropertyName is the name of the dynamic property you are adding to each object

Setting Dynamic Property Attributes

Use the meta.Dynamicproperty object associated with the dynamic property to set property attributes. For example:

P.Hidden = true;

You can remove the dynamic property by deleting its meta.Dynamicproperty object:

delete(P);

The property attributes Constant, Sealed, and Abstract have no meaning for dynamic properties and setting the value of these attributes to true has no effect.

Attaching Data to the Object

Suppose, for example, you are using a predefined set of GUI widget classes (e.g., buttons, sliders, check boxes, etc.) and you want to store the location on a grid of each instance of the widget class. The widget classes might not be designed to store location data for your particular layout scheme and you want to avoid creating a map or hash table to maintain this information separately.

Assuming the button class is a subclass of dynamicprops, you could add a dynamic property to store your layout data. Here is a very simple class to create a uicontrol button:

classdef button < dynamicprops
   properties
      UiHandle
   end
   methods
      function obj = button(pos)
         obj.UiHandle = uicontrol('Position',pos);
      end     
   end   
end

Create an instance of the button class, add a dynamic property, and set its value:

b1 = button([20 40 80 20]); % button class uses HG-type position layout
b1.addprop('myCoord');  % Add a dynamic property
b1.myCoord = [2,3];     % Set the property value

You can access the dynamic property just like any other property, but only on the instance for which it has been defined:

>> b1.myCoord 

ans =

     2     3

Defining Property Access Methods for Dynamic Properties

Dynamic properties enable you to add properties to class instances without modifying class definitions. You can also define property set access or get access methods without creating new class methods. See Property Access Methods for more on the purpose and techniques of these methods.

Here are the steps for creating a property access methods:

Suppose you want to create a property set function for the button class dynamic property myCoord created above. The function might be written as follows:

function set_myCoord(obj,val)
   if  ~(length(val) == 2) % require two values
      error('myCoords require two values ')
   end
   obj.myCoord = val; % set property value
end 

Because button is a handle class, the property set function does not need to return the object as an output argument. You simply assign the value to the property if the value is value is valid.

Use the handle class method findprop to get the meta.Dynamicproperty object:

mb1 = b1.findprop('myCoord');
mb1.SetMethod = @set_myCoord;

The property set function is now called whenever you set this property:

b1.myCoord = [1 2 3] % length must be two
??? Error using ==> set_myCoord at 3
myCoords require two values

Dynamic Properties and 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. This means that if you delete a dynamic property, and then create another one with the same name, the listeners do not respond to events generated by the new property even though the property has the same name as the property for which the event was defined.

Having a listener defined for a deleted dynamic property 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.

  


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