Use the addprop
method to add dynamic properties
to a class that is derived from the dynamicprops
class.
The save
function saves dynamic
properties with the object to which they are attached. For more information
on dynamic properties, see Dynamic Properties — Adding Properties to an Instance.
saveobj
and loadobj
MethodsThe save
function saves dynamic properties
and their values. However, save
does not save dynamic
property attributes because these attributes are not specified in
the class definition. If you save an object that has dynamic properties
with nondefault attributes values, use saveobj
and loadobj
to
manage the saving and loading of attribute values.
If the dynamic property has nondefault attribute values, convert
the object to a struct
in the saveobj
method.
Save the dynamic property attribute values in the struct
so
that the loadobj
method can restore these values.
saveobj
and loadobj
MethodsYour saveobj
method can obtain the nondefault
attribute values from the meta.DynamicProperty
object
associated with the dynamic property. Suppose the object that you
are saving has a dynamic property called DynoProp
.
Create a struct
in the saveobj
method
to save the data that the loadobj
method uses to
reconstruct the object.
Here is how the saveobj
method works:
Obtain the meta.DynamicProperty
object
for the dynamic property.
Store the name and value of the dynamic property in struct
s
.
Store the nondefault dynamic property attributes values
for SetAccess
and GetAccess
in
the struct
. The loadobj
function
restores these values.
methods function s = saveobj(obj) metaDynoProp = findprop(obj,'DynoProp'); s.dynamicprops(1).name = metaDynoProp.Name; s.dynamicprops(1).value = obj.DynoProp; s.dynamicprops(1).setAccess = metaDynoProp.SetAccess; s.dynamicprops(1).getAccess = metaDynoProp.GetAccess; ... end end
Your loadobj
method can add the dynamic
property and set the attribute values:
Create an instance of the class.
Use addprop
to add a new dynamic
property to the object.
Restore the attributes of the dynamic property.
methods (Static) function obj = loadobj(s) if isstruct(s) obj = ClassConstructor; ... metaDynoProp = addprop(obj,s.dynamicprops(1).name); obj.(s.dynamicprops(1).name) = s.dynamicprops(1).value; metaDynoProp.SetAccess = s.dynamicprops(1).setAccess; metaDynoProp.GetAccess = s.dynamicprops(1).getAccess; end end end