Main Content

Dynamic Properties — Adding Properties to an Instance

You can add properties to instances of classes that derive from the dynamicprops class. Use a dynamic property to attach a new, temporary property to a specific instance of a class. Dynamic properties can store temporary data that is not needed for all instances of that 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.

P = addprop(A,"PropertyName")
  • A is an array of objects whose class is derived from dynamicprops.

  • PropertyName is the name of the dynamic property to add to each object.

  • P is an array of matlab.metadata.DynamicProperty objects.

Create a Dynamic Property and Set Its Value

The button class creates a uicontrol button. The button class inherits from dynamicprops.

classdef button < dynamicprops
    properties
        UiHandle
    end
    methods
        function obj = button
            obj.UiHandle = uicontrol(Style="pushbutton");
        end
    end
end

This class is not designed to store location data for any particular layout scheme, but you can use a dynamic property to add that information to an instance. Create an instance b1, and add a dynamic property myCoord to the instance to store your custom layout coordinates.

b1 = button;
dynprop = b1.addprop("myCoord");
b1.myCoord = [2,3];

Access the dynamic property just like any other property, but only on the instance for which you defined it.

b1.myCoord
ans =

     2     3

Set Dynamic Property Attributes

To set property attributes, use the matlab.metadata.DynamicProperty object associated with the dynamic property. For example, set the Hidden attribute of the dynamic property myCoord to true using the dynprop object.

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

You can set the GetAccess and SetAccess attributes of a dynamic property to a metaclass object or cell array of metaclass objects. (since R2026a) For example, restrict the GetAccess attribute of the myCoord dynamic property to instances of a class called TestClass.

dynprop.GetAccess = ?TestClass
dynprop = 

  DynamicProperty with properties:

                    Name: 'myCoord'
             Description: ''
     DetailedDescription: ''
               GetAccess: {[1×1 matlab.metadata.Class]}
               SetAccess: 'public'
               Dependent: 0
                Constant: 0
                Abstract: 0
               Transient: 0
                  Hidden: 1
                  ...

The access is defined in terms of the class that owns the dynamic property, not the code that created the dynamic property. For example, setting GetAccess of myCoord to TestClass in the previous example means that only TestClass instances can retrieve the property value. If the code that created myCoord is not part of TestClass, that code no longer has read access.

Remove a Dynamic Property

Remove a dynamic property by deleting its matlab.metadata.DynamicProperty object. For example, remove the myCoord property of the button object b1 by deleting the dynprop object.

delete(dynprop)

Naming Dynamic Properties

Dynamic property names must be unique and cannot match the name of a class-defined property or method. Because multiple programs can attach dynamic properties to the same object, choose unique names to avoid name conflicts. Dynamic property names must also be valid MATLAB identifiers (see Variable Names) and cannot be the same name as a method of the class. 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 functions that support array functionality: empty, transpose, ctranspose, permute, reshape, display, disp, details, or sort

Determine If a Property Is Dynamic

To identify whether a property is dynamic, use findprop to get the metadata object for that property. For example, use findprop to get the metadata for the myCoord property of the button object b1. The class of the return value is matlab.metadata.DynamicProperty, so the property is dynamic.

mp = findprop(b1,"myCoord");
class(mp)
ans =

    'matlab.metadata.DynamicProperty'

See Also

Topics