Find Default Values in Property Metadata - MATLAB & Simulink (original) (raw)

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 amatlab.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 thePropertyWithDefault class shown here:

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

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

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

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

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:

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 theDefaultValue property if the class does not define a default value for the property.
  3. Obtaining the default value from thematlab.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 withchar vector variable) to obtain amatlab.metadata.Class object.

The matlab.metadata.Class objectPropertyList contains an array ofmatlab.metadata.Property objects. Identify which property corresponds to which matlab.metadata.Property object using thematlab.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:
  2. Get an array of matlab.metadata.Property objects from thematlab.metadata.Class PropertyList property:
  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 ifMyClass 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 thematlab.metadata.Property HasDefault property before querying theDefaultValue 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

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

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:

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

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 propertyFoo has a value of true for its HasDefault property. Accessing DefaultValue returns the value[]:

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

See Also

Topics