Since MATLAB® is an untyped language, most functions do not require argument
declarations or validation. However, if your function has wide usage and you
need to verify the type, size, or other aspects of inputs to ensure that
your code works as expected, you can define an
arguments block (since R2019b). For details, see Function Argument Validation.
Additionally, you can check how many inputs or outputs your function receives, the names of input variables, and more.
Declare and Validate Arguments
|Declare function argument validation (Since R2019b)
Numeric Value Validation
|Validate that value is positive
|Validate that value is nonpositive
|Validate that value is nonnegative
|Validate that value is negative
|Validate that value is finite
|Validate that value is not NaN
|Validate that value is nonzero
|Validate that value is nonsparse
|Validate that value is sparse (Since R2023b)
|Validate that value is real
|Validate that value is integer
|Validate that value is not missing (Since R2020b)
|Validate that value is greater than another value
|Validate that value is less than another value
|Validate that value is greater than or equal to another value
|Validate that value is less than or equal to another value
|Validate that value comes from one of specified classes (Since R2020b)
|Validate that value is numeric
|Validate that value is numeric or logical
|Validate that value is floating-point array (Since R2020b)
|Validate that value is string array, character vector, or cell array of character vectors (Since R2020b)
|Validate that value is single piece of text (Since R2020b)
|Validate that value is text with nonzero length (Since R2020b)
|Validate that value has specified underlying type (Since R2020b)
|Validate that value is nonempty
|Validate that value is scalar or empty (Since R2020b)
|Validate that value is vector (Since R2020b)
Range and Set Membership
|Validate that value is in the specified range (Since R2020b)
|Validate that value is member of specified set
|Validate that path refers to file (Since R2020b)
|Validate that input path refers to folder (Since R2020b)
|Validate that input name is valid variable name (Since R2020b)
Name-Value Arguments Structure
|Convert structure containing name-value pairs to cell array (Since R2019b)
Variable-Length Argument Lists
- Function Argument Validation
Declare argument class and size, and enforce restrictions on argument values.
- Argument Validation Functions
Validate specific requirements of arguments using validation functions. Write your own functions to check for specific argument requirements.
- Check Function Inputs with validateattributes
This example shows how to verify that the inputs to your function conform to a set of requirements using the
- Parse Function Inputs
Define required and optional inputs, assign defaults to optional inputs, and validate all inputs to a custom function using the Input Parser.
Number of Arguments
- Support Variable Number of Inputs
Define a function that accepts a variable number of input arguments using
vararginargument is a cell array that contains the function inputs, where each input is in its own cell.
- Support Variable Number of Outputs
Define a function that returns a variable number of output arguments using
varargoutis a cell array that contains the function outputs, where each output is in its own cell.
- Find Number of Function Arguments
nargoutto determine how many input or output arguments your function receives.
- Ignore Inputs in Function Definitions
If your function accepts a predefined set of inputs, but does not use all the inputs, use the tilde (
~) operator to ignore them in your function definition.