MATLAB Object-Oriented Programming

(Joyce) #1

(^2) Testing the logical value of the meta.property HasDefault property to determine
if the property defines a default value. MATLAB returns an error when you query the
DefaultValue property if the class does not define a default value for the property.
(^3) Obtaining the default value from the meta.property DefaultValue property if the
HasDefault value is true.
Use the? operator, the metaclass function, or the meta.class.fromName static
method (works with char vector variable) to obtain a meta.class object.
The meta.class object PropertyList contains an array of meta.property objects.
Identify which property corresponds to which meta.property object using the
meta.property Name property.
For example, this class defines properties with default values:
classdef MyDefs
properties
Material = 'acrylic'
InitialValue = 1.0
end
end
Follow these steps to obtain the default value defined for the Material property. Include
any error checking that is necessary for your application.
1 Get the meta.class object for the class:
mc = ?MyDefs;
2 Get an array of meta.property objects from the meta.class PropertyList
property:
mp = mc.PropertyList;
3 The length of the mp array equals the number of properties. You can use the
meta.property Name property to find the property of interest:
for k = 1:length(mp)
if (strcmp(mp(k).Name,'Material')
...
4 Before querying the default value of the Material property, test the HasDefault
meta.property to determine if MyClass defines a default property for this
property:
16 Information from Class Metadata

Free download pdf