主要内容

matlab.metadata.ArgumentValidator Class

Namespace: matlab.metadata

Describe argument validation functions applied to function arguments

Since R2026a

Description

The matlab.metadata.ArgumentValidator class describes a validation function applied to an input or output argument of a function or method. The class includes information about any other arguments the validation function references.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

You cannot instantiate a matlab.metadata.ArgumentValidator object directly. The Functions property of matlab.metadata.ArgumentValidation is of type ArgumentValidator.

Properties

expand all

The name of the validation function applied to a function or method argument, specified as a character vector.

Attributes:

GetAccess
public
SetAccess
private

Handle for the validation function.

Attributes:

GetAccess
public
SetAccess
private

Arguments that are referenced by the validation function, specified as a matlab.metadata.ArgumentIdentifier array.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Save the function file trimData.m to your path.

function revisedData = trimData(dataSet,lowerLimit)
% trimData Remove all items from a numeric data set that are less than
% or equal to a given value.

    arguments (Input)
        dataSet {mustBeNumeric}
        lowerLimit {mustBeScalarOrEmpty,mustBeInDataRange(lowerLimit,dataSet)}
    end

    ind = dataSet > lowerLimit;
    revisedData = dataSet(ind);
end

function mustBeInDataRange(dataPoint,dataSet)
    if ((dataPoint < min(dataSet)) || (dataPoint > max(dataSet)))
        eidType = 'mustBeInDataRnage:notInDataRange';
        msgType = 'Data point must be inside the range of the data set.';
        error(eidType,msgType)
    end
end

To get information about the function using introspection, call metafunction to create a matlab.metadata.Function instance for trimData. Access the Signature property of Function, and then access the second input argument.

mf = metafunction("trimData");
ms = mf.Signature;
mi = ms.Inputs(2)
mi = 

  Argument with properties:

             Identifier: lowerLimit
            Description: ''
    DetailedDescription: ''
               Required: 1
              Repeating: 0
              NameValue: 0
             Validation: [1×1 matlab.metadata.ArgumentValidation]
           DefaultValue: [0×0 matlab.metadata.DefaultArgumentValue]
            SourceClass: [0×0 matlab.metadata.Class]

Access the Validation property. The property shows that two validation functions are applied to lowerLimit.

mv = mi.Validation
mv = 

  ArgumentValidation with properties:

        Class: [0×0 matlab.metadata.Class]
         Size: [1×0 matlab.metadata.ArrayDimension]
    Functions: [1×2 matlab.metadata.ArgumentValidator]

Access the second element of the Functions property. The matlab.metadata.ArgumentValidator instance shows the name of the local validation function for the lowerLimit argument, mustBeInDataRange, and which arguments the function references. ArgumentValidator also returns an anonymous function handle to mustBeInDataRange.

mv.Functions(2)
ans = 

  ArgumentValidator with properties:

                   Name: 'mustBeInDataRange'
    ReferencedArguments: [lowerLimit    dataSet]
               Function: @(lowerLimit,dataSet)mustBeInDataRange(lowerLimit,dataSet)

Assign the function handle in Function to a variable and call mustBeInDataRange at the command line. To see an example of the error the function generates, call it with dataPoint and dataSet arguments that do not satisfy the validation.

val = mv.Functions(2).Function;
val(-1,1:5)
Error using trimData>mustBeInDataRange (line 18)
Data point must be inside the range of the data set.

Version History

Introduced in R2026a