Main Content

Argument Definitions

Accept a variable number of inputs or outputs, check for valid values

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.

Functions

expand all

Arguments Block

argumentsDeclare function argument validation (Since R2019b)

Numeric Value Validation

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
mustBeSparseValidate that value is sparse (Since R2023b)
mustBeRealValidate that value is real
mustBeIntegerValidate that value is integer
mustBeNonmissingValidate that value is not missing (Since R2020b)

Comparisons

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

Data Types

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

Size

mustBeNonemptyValidate that value is nonempty
mustBeScalarOrEmptyValidate that value is scalar or empty (Since R2020b)
mustBeVectorValidate that value is vector (Since R2020b)

Range and Set Membership

mustBeInRangeValidate that value is in the specified range (Since R2020b)
mustBeMemberValidate that value is member of specified set

Names

mustBeFileValidate that path refers to file (Since R2020b)
mustBeFolderValidate that input path refers to folder (Since R2020b)
mustBeValidVariableNameValidate that input name is valid variable name (Since R2020b)

Name-Value Arguments Structure

namedargs2cellConvert structure containing name-value pairs to cell array (Since R2019b)

Inputs

vararginVariable-length input argument list
narginNumber of function input arguments
narginchkValidate number of input arguments

Outputs

varargoutVariable-length output argument list
nargoutNumber of function output arguments
nargoutchkValidate number of output arguments
validateattributesCheck validity of array
validatestringCheck validity of text
validatecolorValidate color values (Since R2020b)
inputnameVariable name of function input
mfilenameFile name of currently running code
inputParserInput parser for functions

Topics

Argument Validation

Number of Arguments

Pass-Through Inputs

  • 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.