Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Find Default Values in Property Metadata

Default Values

Class definitions can specify explicit default values for properties (see Property Default Values). You can determine if a class defines explicit default values for a property and what the value of the default is from the property's meta.property object.

meta.property Data

The meta.class object for a class contains a meta.property object for every property defined by the class, including properties with private and protected access.

For example. get the meta.class object for MATLAB® MException class:

mc = ?MException;

Get an array of meta.property objects from the meta.class object:

mp = mc.PropertyList;

The first element in the mp array is the meta.property object for the type property:

mp(1)
ans = 

  class with properties:

                   Name: 'type'
            Description: 'Type of error reporting'
    DetailedDescription: ''
              GetAccess: 'private'
              SetAccess: 'private'
              Dependent: 0
               Constant: 0
               Abstract: 0
              Transient: 0
                 Hidden: 0
          GetObservable: 1
          SetObservable: 1
               AbortSet: 0
              GetMethod: []
              SetMethod: []
             HasDefault: 1
           DefaultValue: {}
          DefiningClass: [1x1 meta.class]

Two meta.property properties provide information on default values:

  • HasDefaulttrue if class specifies a default value for the property, false if it does not.

  • DefaultValue — Contains the default value, if the class defines a default value for the property.

These properties provide a programmatic way to obtain property default values without reading class definition files. Use these meta.property object properties to obtain property default values for both built-in classes and classes defined in MATLAB code.

Query Default Value

The procedure for querying a default value involves:

  1. Getting the meta.property object for the property whose default value you want to query.

  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 property 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:

          if mp(k).HasDefault
             dv = mp(k).DefaultValue;
          end
       end
    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 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 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
ans =

    0
dv = mp.DefaultValue;
No default value has been defined for property Foo

Abstract Property

MyClass defines the Foo property as Abstract:

classdef MyAbst
   properties (Abstract)
      Foo
   end
end

The meta.property instance for property Foo has a value of false for its HasDefault property because you cannot define a default value for an Abstract property. Attempting to access DefaultValue causes an error:

mc = ?MyAbst;
mp = mc.PropertyList(1);
mp.HasDefault
ans =

    0
dv = mp.DefaultValue;
Property Foo is abstract and therefore cannot have a default value.

Property With Expression That Errors

MyPropEr defines the Foo property default value as an expression that errors when evaluated.

classdef MyPropEr
   properties
      Foo = sin(pie/2);
   end
end

The meta.property object for property Foo has a value of true for its HasDefault property because Foo does have a default value:

sin(pie/2)

However, this expression returns an error (pie is a function that creates a pie graph, not the value pi).

mc = ?MyPropEr;
mp = mc.PropertyList(1);
mp.HasDefault
ans =

    1
dv = mp.DefaultValue;
Error using pie (line 29)
Not enough input arguments.

Querying the default value causes the evaluation of the expression and returns the error.

Property With Explicitly Defined Default Value of Empty ([])

MyEmptyProp assigns a default of [] (empty double) to the Foo property:

classdef MyEmptyProp
   properties
      Foo = [];
   end
end

The meta.property object for property Foo has a value of true for its HasDefault property. Accessing DefaultValue returns the value []:

mc = ?MyEmptyProp;
mp = mc.PropertyList(1);
mp.HasDefault
ans =

    1
dv = mp.DefaultValue;
dv =

    []

Related Examples

Was this topic helpful?