arguments
Declare function argument validation
Syntax
argumentsargName1 (dimensions) class {validators} = defaultValue
...argNameN ...
end arguments (Repeating)argName1 (dimensions) class {validators}
...argNameN ...
end arguments (Output)argName1 (dimensions) class {validators}
...argNameN ...
end arguments (Output,Repeating)argName (dimensions) class {validators}
end
Description
Input Argument Blocks
arguments ... end
declares input arguments for a function. The
arguments block is optional. If you include one or more arguments
blocks, they must appear before the first executable line of the function. MATLAB® treats any argument block that is not labeled explicitly with
Input
or Output
as an input block.
Each argument can have one or more restrictions or a default value, as shown in this syntax:
argName (dimensions) class {validators} =
defaultValue
— Input size, specified as a comma-separated list of two or more numbers, such as(dimensions)
(1,2)
,(3,5,2)
, or(1,:)
. A colon allows any length in that dimension.
cannot include expressions.(dimensions)
The dimensions of the input must match
exactly or be compatible with the size specified by(dimensions)
. For example,(dimensions)
(1,:)
specifies the input must be a 1-by-n row vector, but an n-by-1 column vector is compatible. The function reshapes a row vector input into a column vector. Similarly, a size of(2,3)
allows scalar input, but it expands the input to a 2-by-3 matrix. See Compatible Array Sizes for Basic Operations for more information.
— Class or MATLAB data type specified by name, such asclass
double
. The input must be the specified type or a type that can be converted to that type. For example, a function that specifiesdouble
accepts values of classsingle
and converts them todouble
. For more information on conversions, see Implicit Class Conversion.
— Comma-separated list of validation functions, such as{validators}
mustBeNumeric
andmustBeScalarOrEmpty
, enclosed in curly brackets. Validation functions error when the input arguments do not match their conditions. Unlike
, validation functions do not modify input arguments. For a list of validation functions, see Argument Validation Functions.class
— Default values must conform to the specified size, type, and validation rules. A default value can also be an expression. Specifying a default value makes the argument optional. Optional arguments must be positioned after required arguments in the function signature and in thedefaultValue
arguments
block.
For name-value arguments,
uses the
form arg
, where
nv.name
is a structure name in the function
signature and nv
is the argument name in
the arguments block. For instance, define a function that accepts name-value arguments
using a structure named name
options
.
y = myFunction(x,options)
In the arguments block, specify the names for name-value arguments as fields:
arguments x options.Name1 options.Name2 end
For more information on using arguments
blocks in general, see
arguments Block Syntax.
arguments (Repeating) ... end
declares repeating input
arguments.
For example, if you create a function named myplot
with repeating
arguments X
, Y
, and style
, the
function accepts multiple sets of these three arguments, such as
myplot(x1,y1,style1,x2,y2,style2)
. MATLAB creates a cell array that contains all the values passed in for that
argument.
Functions can include only one repeating input arguments block. If the function includes both repeating and name-value arguments, declare name-value arguments in their own, separate arguments block after the repeating arguments block.
For more information on using validation with repeating arguments, see Validate Repeating Arguments.
Output Argument Blocks
arguments (Output) ... end
declares output arguments for a
function. The output arguments block is optional. If you include one or more output
arguments
blocks, they must appear after all input blocks but before
the first executable line of the function. When including both input and output blocks in
a function, including the (Input)
and (Output)
attributes explicitly is recommended for readability. See Repeating Outputs with Argument Validation for an example.
(since R2022b)
Like input arguments, output arguments can have one or more restrictions, as shown in this syntax:
argName (dimensions) class
{validators}
See the description for arguments ... end
for additional details.
Unlike input arguments, output arguments cannot define a default value, and validation
functions applied to an output argument cannot reference an earlier output
argument.
arguments (Output,Repeating) ... end
declares a repeating output
argument for a function. You can use argument validation for a repeating output argument,
but you can define only one repeating output argument per function.
varargout
can appear in a repeating output arguments block as long
as it is the only output argument. (since R2022b)
Examples
Limitations
Argument blocks are not supported in nested functions, abstract methods, or handle class destructor methods.
More About
Tips
Using data type restrictions can result in implicit conversions of input arguments. For example:
For this function, if you pass the stringfunction y = myFunction(inputArg1) arguments inputArg1 (1,1) double end ...
"123"
as the input argument, MATLAB converts the string to the numeric value123
of typedouble
.Validation functions do not change input values in any way, so to avoid data type conversion, use one or more validator functions instead of a data type to restrict the input. For example:
To avoid conversion of strings to numeric values, use
mustBeA
,mustBeFloat
, ormustBeNumeric
.To avoid conversion of numeric values to strings, use
mustBeText
,mustBeTextScalar
, ormustBeNonZeroLengthText
.To avoid size conversions, use
mustBeVector
ormustBeScalarOrEmpty
.
MATLAB is able to provide code completions and suggestions for functions with
arguments
blocks based on the information contained in the arguments block. This information is available without requiring afunctionSignatures.json
file. For more information on customizing code suggestions and completions see, Customize Code Suggestions and Completions.