Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

Input and Output Arguments

Support variable length argument lists, check arguments

Functions

nargin Number of function input arguments
nargout Number of function output arguments
varargin Variable-length input argument list
varargout Variable-length output argument list
narginchk Validate number of input arguments
nargoutchk Validate number of output arguments
validateattributes Check validity of array
validatestring Check validity of text
inputParser Parse function inputs
inputname Variable name of function input

Examples and How To

Find Number of Function Arguments

Use nargin and nargout to determine how many input or output arguments your function receives.

Support Variable Number of Inputs

Define a function that accepts a variable number of input arguments using varargin. The varargin argument 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 varargout. Output varargout is a cell array that contains the function outputs, where each output is in its own cell.

Validate Number of Function Arguments

Check if your custom function receives a valid number of input or output arguments. MATLAB® performs some argument checks automatically. For other cases, you can use narginchk or nargoutchk.

Ignore Function Inputs

If your function accepts a predefined set of inputs, but does not use all the inputs, the tilde (~) operator to ignore them in your function definition.

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 validateattributes function.

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.

Concepts

Argument Checking in Nested Functions

There are special considerations for using varargin, varargout, nargin, and nargout with nested functions.

Input Parser Validation Functions

To check custom function inputs, define validation functions that you pass to the Input Parser.

Was this topic helpful?