Main Content

Generate Code for arguments Block That Validates Input Arguments

You can generate code for arguments blocks that perform input argument validation in your MATLAB® function. Input argument validation declares specific restrictions on function input arguments. Using argument validation, you can constrain the class, size, and other aspects of function input values without writing code in the body of the function to perform these tests. See Function Argument Validation.

Supported Features

Code generation supports most features of arguments blocks, including size and class validation, validation functions, and default values.

Code generation supports only varargin as a repeating argument. For varargin, size validation, class validation, and validation functions are not supported for code generation.

Code generation does not support these features of arguments blocks:

  • Repeating arguments other than varargin

  • Name-value arguments

  • Output argument validation

Input Type Specification and arguments blocks

Using function argument validation (arguments blocks) to specify input types of entry-point functions is not supported. Even if your entry-point function contains arguments blocks that validate the input arguments, you must specify the properties of these input arguments by using one of the three approaches listed in Methods for Defining Properties of Primary Inputs (MATLAB Coder).

Default Values for Entry-Point Function Inputs in Generated Code

The arguments block allows you to specify default values for one or more positional input arguments. Specifying a default value in the argument declaration makes a positional argument optional because MATLAB can use the default value when you do not pass a value in the function call. When you generate code by using the codegen (MATLAB Coder) command or accelerate fixed-point code by using the fiaccel command, you can choose to not specify the properties of one or more optional positional arguments that have constant default values. In such situations, the default values of these optional arguments are hard-coded in the generated code and these arguments do not appear in the generated code interface. For examples, see the following table.

MATLAB Code

Generated Code

function out = useDefaults_1(a,b,c)
arguments
    a (1,1) double = 3
    b (1,1) double = 5
    c (1,1) double = 7
end
out = a + b + c;
end

codegen command:

codegen -config:lib -c useDefaults_1 -args {} -report

Generated code:

double useDefaults_1(void)
{
  return 15.0;
}

function out = useDefaults_2(a,b,c)
arguments
    a (1,1) double
    b (1,1) double = 5
    c (1,1) double = 7
end
out = a + b + c;
end

codegen command:

codegen -config:lib -c useDefaults_2 -args 0 -report

Generated code:

double useDefaults_2(double a)
{
  return (a + 5.0) + 7.0;
}

codegen command:

codegen -config:lib -c useDefaults_2 -args {0,0} -report

Generated code:

double useDefaults_2(double a, double b)
{
  return (a + b) + 7.0;
}

See Also

|

Related Topics