Declare function argument validation
argumentsargName1 (dimensions) dataType {validators} = defaultValue
...argNameN ...
end arguments (Repeating) ... end
arguments ... end
declares input arguments for a function. The
arguments block is optional. If you include an arguments
block, it must
appear before the first executable line of the function. Functions can include multiple
arguments
blocks.
Each argument can have one or more restrictions or a default value, as shown in this syntax:
argName (dimensions) dataType {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.
— Data type,
specified as a class name, such as dataType
double
. The input must be the
specified type or a type that can be converted to that type. For example, a function
that specifies double
accepts values of type single
and
converts them to double
.
— Comma-separated list
of validation functions, such as {validators}
mustBeNumeric
and
mustBeScalar
, 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.dataType
— 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 the defaultValue
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 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 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 repeating arguments, see Repeating Arguments.
Argument blocks are not supported in nested functions, abstract methods, or handle class destructor methods.
Using data type restrictions can result in implicit conversions of input arguments. For example:
function y = myFunction(inputArg1) arguments inputArg1 (1,1) double end ...
"123"
as the input argument,
MATLAB converts the string to the numeric value 123
of type
double
.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
, or
mustBeNumeric
.
To avoid conversion of numeric values to strings, use
mustBeText
, mustBeTextScalar
, or
mustBeNonZeroLengthText
.
To avoid size conversions, use mustBeVector
or
mustBeScalarOrEmpty
.