Main Content

Find Default Values in Property Metadata

Default Values

Class definitions can specify explicit default values for properties (see Define Properties with Default Values). Determine if a class defines an explicit default value for a property and what the value of the default is from the property matlab.metadata.Property object.

matlab.metadata.Property Data

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

For example, get the matlab.metadata.Class object for the PropertyWithDefault class shown here:

classdef PropertyWithDefault
   properties
      Date = date
      RandNumber = randi(9)
   end
end

Get an array of matlab.metadata.Property objects from the matlab.metadata.Class object:

mc = ?PropertyWithDefault; % matlab.metadata.Class object
mp = mc.PropertyList; % matlab.metadata.Property array

The second element in the mp array is the matlab.metadata.Property object for the RandNumber property. Listing the matlab.metadata.Property object shows the information contained in its properties:

mp(2)
  property with properties:

                   Name: 'RandNumber'
            Description: ''
    DetailedDescription: ''
              GetAccess: 'public'
              SetAccess: 'public'
              Dependent: 0
               Constant: 0
               Abstract: 0
              Transient: 0
                 Hidden: 0
          GetObservable: 0
          SetObservable: 0
               AbortSet: 0
            NonCopyable: 0
              GetMethod: []
              SetMethod: []
             HasDefault: 1
           DefaultValue: 5
          DefiningClass: [1×1 matlab.metadata.Class]

Two of the listed matlab.metadata.Property properties provide information on default values:

  • HasDefaulttrue (displayed as 1) if the class specifies a default value for the property, false if it does not.

  • DefaultValue — Contains the default value, when the class defines a default value for the property. If the default value is an expression, the value of DefaultValue is the result of evaluating the expression.

For more information on the evaluation of property default values defined by expressions, see Evaluation of Expressions in Class Definitions.

These properties provide a programmatic way to obtain property default values without opening class definition files. Use these matlab.metadata.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 matlab.metadata.Property object for the property whose default value you want to query.

  2. Testing the logical value of the matlab.metadata.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 matlab.metadata.Property DefaultValue property if the HasDefault value is true.

Use the ? operator, the metaclass function, or the matlab.metadata.Class.fromName static method (works with char vector variable) to obtain a matlab.metadata.Class object.

The matlab.metadata.Class object PropertyList contains an array of matlab.metadata.Property objects. Identify which property corresponds to which matlab.metadata.Property object using the matlab.metadata.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 matlab.metadata.Class object for the class:

    mc = ?MyDefs;

  2. Get an array of matlab.metadata.Property objects from the matlab.metadata.Class PropertyList property:

    mp = mc.PropertyList;
  3. The length of the mp array equals the number of properties. You can use the matlab.metadata.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 matlab.metadata.Property to determine if MyClass defines a default value for this property:

    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 matlab.metadata.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 matlab.metadata.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 matlab.metadata.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 matlab.metadata.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 matlab.metadata.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 matlab.metadata.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 Topics