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.
Once defined, dynamic properties behave much like class-defined properties:
Set and query the values of dynamic properties using dot notation. (See Assign Data to the Dynamic Property.)
MATLAB saves and loads dynamic properties when you save and load the objects to which they are attached. (See Dynamic Properties and ConstructOnLoad.)
Define attributes for dynamic property. (See Set Dynamic Property Attributes).
MATLAB does not save property attribute values when you save objects that have dynamic properties. Therefore, any nondefault attribute setting are not retained when you load the object. (See Save and Load Dynamic Properties.)
By default, dynamic properties have their NonCopyable
attribute
set to true
. If you copy an object containing a
dynamic property, the dynamic property is not copied. (See Objects with Dynamic Properties)
Add property set and get access methods. (See Access Methods for Dependent Properties.)
Listen for dynamic property events. (See Dynamic Property Events.)
Access dynamic property values from object arrays, with restricted syntax. (See Accessing Dynamic Properties in Arrays.)
The isequal
function
always returns false
when comparing objects that
have dynamic properties, even if the properties have the same name
and value. To compare objects that contain dynamic properties, overload isequal
for
your class.
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
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 (.
)
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 the dynamic property by deleting its meta.DynamicProperty
object:
delete(P);
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
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 a of 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 meta.DynamicProperty
.