Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Dynamic Properties — Adding Properties to an Instance

What Are Dynamic Properties

You can add properties to instances of classes that derive from the dynamicprops class. These dynamic properties are sometimes referred to as instance properties. Use dynamic properties to attach temporary data to objects or to assign data that you want to associate with an instance of a class, but not all objects of that class.

It is possible for more than one program to define dynamic properties on the same object. In these cases, avoid name conflicts. Dynamic property names must be valid MATLAB® identifiers (see Variable Names) and cannot be the same name as a method of the class.

Characteristics of Dynamic Properties

Once defined, dynamic properties behave much like class-defined properties:

Define 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

Name Dynamic Properties

Use only valid names when naming dynamic properties (see Variable Names). In addition, do not use names that:

  • Are the same as the name of a class method

  • Are the same as the name of a class event

  • Contain a period (.)

Set Dynamic Property Attributes

To set property attributes, use the meta.DynamicProperty object associated with the dynamic property. For example, if P is the object returned by addprop, this statement sets the property's Hidden attribute to true:

P.Hidden = true;

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

Remove a Dynamic Property

Remove the dynamic property by deleting its meta.DynamicProperty object:

delete(P);

Assign Data to the Dynamic Property

Suppose, you are using a predefined set of user interface widget classes (buttons, sliders, check boxes, etc.). You want to store the location of each instance of the widget class. Assume that the widget classes are not designed to store location data for your particular layout scheme. You want to avoid creating a map or hash table to maintain this information separately.

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

classdef button < dynamicprops
   properties
      UiHandle
   end
   methods
      function obj = button(pos)
         if nargin > 0
            if length(pos) == 4
               obj.UiHandle = uicontrol('Position',pos,...
                  'Style','pushbutton');
            else
               error('Improper position')
            end
         end
      end
   end
end

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

b1 = button([20 40 80 20]);
b1.addprop('myCoord');
b1.myCoord = [2,3];

Access the dynamic property just like any other property, but only on the object on which you defined it:

b1.myCoord
ans =

     2     3

Related Examples

Was this topic helpful?