Main Content

La traducción de esta página está obsoleta. Haga clic aquí para ver la última versión en inglés.

Argumentos de entrada y salida

Admitir listas de argumentos de longitud variable, comprobar argumentos, declarar argumentos de validación

Las funciones pueden aceptar números variables de entradas y salidas mediante una sola firma de función. MATLAB® ofrece numerosas técnicas para comprobar el número, el tipo, el tamaño y otros aspectos de las entradas para garantizar que las funciones funcionan como se espera, para proporcionar mensajes de error útiles en los casos en que las entradas no sean válidas.

Funciones

expandir todo

narginNumber of function input arguments
nargoutNumber of function output arguments
vararginVariable-length input argument list
varargoutVariable-length output argument list
narginchkValidate number of input arguments
nargoutchkValidate number of output arguments
validateattributesCheck validity of array
validatestringCheck validity of text
validatecolorValidate color values
inputParserInput parser for functions
inputnameVariable name of function input
mfilenameFile name of currently running code
argumentsDeclare function argument validation
namedargs2cellConvert structure containing name-value pairs to cell array

Validación de atributos de valor numérico

mustBePositiveValidate that value is positive
mustBeNonpositiveValidate that value is nonpositive
mustBeNonnegativeValidate that value is nonnegative
mustBeNegativeValidate that value is negative
mustBeFiniteValidate that value is finite
mustBeNonNanValidate that value is not NaN
mustBeNonzeroValidate that value is nonzero
mustBeNonsparseValidate that value is nonsparse
mustBeRealValidate that value is real
mustBeIntegerValidate that value is integer
mustBeNonmissingValidate that value is not missing

Validación de la comparación

mustBeGreaterThanValidate that value is greater than another value
mustBeLessThanValidate that value is less than another value
mustBeGreaterThanOrEqualValidate that value is greater than or equal to another value
mustBeLessThanOrEqualValidate that value is less than or equal to another value

Validación del tipo de datos

mustBeAValidate that value comes from one of specified classes
mustBeNumericValidate that value is numeric
mustBeNumericOrLogicalValidate that value is numeric or logical
mustBeFloatValidate that value is floating-point array
mustBeTextValidate that value is string array, character vector, or cell array of character vectors
mustBeTextScalarValidate that value is single piece of text
mustBeNonzeroLengthTextValidate that value is text with nonzero length
mustBeUnderlyingTypeValidate that value has specified underlying type

Validación de tamaño

mustBeNonemptyValidate that value is nonempty
mustBeScalarOrEmptyValidate that value is scalar or empty
mustBeVectorValidate that value is vector

Validación de miembros y rango

mustBeMemberValidate that value is member of specified set
mustBeInRangeValidate that value is in the specified range

Texto con validación de significado especial

mustBeFileValidate that path refers to file
mustBeFolderValidate that input path refers to folder
mustBeValidVariableNameValidate that input name is valid variable name

Temas

Comprobar número de argumentos

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.

Checking Number of Arguments in Nested Functions

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

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.

Ignorar los valores de salida de una función

Este ejemplo muestra cómo solicitar valores de salida específicos de una función.

Analizar argumentos de entrada

Ways to Parse Function Inputs

Choose a technique for checking the validity of input arguments.

Function Argument Validation

Declare input argument class and size and enforce restrictions on argument values.

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.