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:
Set and query the values of dynamic properties using dot notation. (See Create a Dynamic Property and Set Its Value.)
MATLAB® saves and loads dynamic properties when you save and load the objects to which they are attached. (See Dynamic Properties and ConstructOnLoad.)
Set the values of attributes for dynamic properties. (See Set Dynamic Property Attributes.)
By default, dynamic properties have their
NonCopyableattribute set totrue. (See Objects with Dynamic Properties.)Add property set and get access methods. (See Set and Get Methods for Dynamic Properties.)
Listen for dynamic property events. (See Dynamic Property Events.)
Access dynamic properties of objects contained within object arrays. (See Accessing Properties and Methods in Object Arrays.)
The
isequalfunction always returnsfalsewhen comparing objects that have dynamic properties, even if the properties have the same name and value. To compare objects that contain dynamic properties, overloadisequalfor your class.
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")
Ais an array of objects whose class is derived fromdynamicprops.PropertyNameis the name of the dynamic property to add to each object.Pis an array ofmatlab.metadata.DynamicPropertyobjects.
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 3Set 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, orsort
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'