Main Content

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 matlab.metadata.DynamicProperty objects

H is an array of handles

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

Naming 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 (.)

  • Are the names of function that support array functionality: empty, transpose, ctranspose, permute, reshape, display, disp, details, or sort.

Set Dynamic Property Attributes

To set property attributes, use the matlab.metadata.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 matlab.metadata.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

Access Attribute for Dynamic Properties

Using nonpublic Access with dynamic properties is not recommended because these properties belong to specific instances that are often created outside of class methods. The Access attribute of a dynamic property applies to the class of the instance that contains the dynamic property. The dynamic property Access attribute does not necessarily apply to the class whose method adds the dynamic property.

For example, if a base class method adds a dynamic property with private access to an instance, the private access applies only to the class of the instance.

For more information on dynamic property attributes, see matlab.metadata.DynamicProperty. Use the handle findprop method to get the matlab.metadata.DynamicProperty object.

List Object Dynamic Properties

You can list the dynamic properties for an object using the handle findprop method. Here are the steps:

  • Get the names of the object's properties using the properties function.

  • Get the metadata object for each property using findprop.

  • Use the isa function to determine if the metadata object is a matlab.metadata.DynamicProperty object. If so, then the property is a dynamic property.

The getDynamicPropNames function shows how to display the names of any dynamic properties defined for the input obj.

function getDynamicPropNames(obj)
    % Find dynamic properties
    allprops = properties(obj);
    for i=1:numel(allprops)
        m = findprop(obj,allprops{i});
        if isa(m,matlab.metadata.DynamicProperty')
            disp(m.Name)
        end
    end
end

Related Topics