MATLAB Object-Oriented Programming

(Joyce) #1
if mp(k).HasDefault
dv = mp(k).DefaultValue;
end

The DefaultValue property is read-only. Changing the default value in the class
definition changes the value of DefaultValue property. You can query the default value
of a property regardless of its access settings.


Abstract and dynamic properties cannot define default values. Therefore, MATLAB
returns an error if you attempt to query the default value of properties with these
attributes. Always test the logical value of the meta.property HasDefault property
before querying the DefaultValue property to avoid generating an error.


Default Values Defined as Expressions


Class definitions can define property default values as MATLAB expressions (see
“Evaluation of Expressions in Class Definitions” on page 6-10 for more information).
MATLAB evaluates these expressions the first time the default value is needed, such as
the first time you create an instance of the class.


Querying the meta.property DefaultValue property causes MATLAB to evaluate a
default value expression, if it had not yet been evaluated. Therefore, querying a property
default value can return an error or warning if errors or warnings occur when MATLAB
evaluates the expression. See “Property with Expression That Errors” on page 16-24 for
an example.


Property with No Explicit Default Value


MyClass does not explicitly define a default value for the Foo property:


classdef MyFoo
properties
Foo
end
end


The meta.property instance for property Foo has a value of false for HasDefault.
Because the class does not explicitly define a default value for Foo, attempting to access
the DefaultValue property causes an error:


mc = ?MyFoo;
mp = mc.PropertyList(1);
mp.HasDefault


Find Default Values in Property Metadata
Free download pdf