Accept a variable number of inputs or outputs, check for valid values
Most functions do not require argument declarations or validation because MATLAB® is an untyped language. 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.
function z = mySharedFunction(x,y,NameValueArgs) arguments x (1,1) double % scalar y double {mustBeVector,mustBePositive} NameValueArgs.A string NameValueArgs.B string = "default" end ...end
Functions
expand all
Declare and Validate Arguments
Arguments Block
arguments | Declare function argument validation |
Numeric Value Validation
mustBePositive | Validate that value is positive |
mustBeNonpositive | Validate that value is nonpositive |
mustBeNonnegative | Validate that value is nonnegative |
mustBeNegative | Validate that value is negative |
mustBeFinite | Validate that value is finite |
mustBeNonNan | Validate that value is not NaN |
mustBeNonzero | Validate that value is nonzero |
mustBeNonsparse | Validate that value is nonsparse |
mustBeSparse | Validate that value is sparse (Since R2023b) |
mustBeReal | Validate that value is real |
mustBeInteger | Validate that value is integer |
mustBeNonmissing | Validate that value is not missing (Since R2020b) |
Comparisons
mustBeGreaterThan | Validate that value is greater than another value |
mustBeLessThan | Validate that value is less than another value |
mustBeGreaterThanOrEqual | Validate that value is greater than or equal to another value |
mustBeLessThanOrEqual | Validate that value is less than or equal to another value |
Data Types
mustBeA | Validate that value comes from one of specified classes (Since R2020b) |
mustBeNumeric | Validate that value is numeric |
mustBeNumericOrLogical | Validate that value is numeric or logical |
mustBeFloat | Validate that value is floating-point array (Since R2020b) |
mustBeText | Validate that value is string array, character vector, or cell array of character vectors (Since R2020b) |
mustBeTextScalar | Validate that value is single piece of text (Since R2020b) |
mustBeNonzeroLengthText | Validate that value is text with nonzero length (Since R2020b) |
mustBeUnderlyingType | Validate that value has specified underlying type (Since R2020b) |
Size
mustBeNonempty | Validate that value is nonempty |
mustBeScalarOrEmpty | Validate that value is scalar or empty (Since R2020b) |
mustBeVector | Validate that value is vector (Since R2020b) |
mustBeRow | Validate that value is row vector (Since R2024b) |
mustBeColumn | Validate that value is column vector (Since R2024b) |
mustBeMatrix | Validate that value is matrix (Since R2024b) |
Range and Set Membership
mustBeInRange | Validate that value is in the specified range (Since R2020b) |
mustBeMember | Validate that value is member of specified set |
Names
mustBeFile | Validate that path refers to file (Since R2020b) |
mustBeFolder | Validate that input path refers to folder (Since R2020b) |
mustBeValidVariableName | Validate that input name is valid variable name (Since R2020b) |
Name-Value Arguments Structure
namedargs2cell | Convert structure containing name-value pairs to cell array |
Variable-Length Argument Lists
Inputs
varargin | Variable-length input argument list |
nargin | Number of function input arguments |
narginchk | Validate number of input arguments |
Outputs
varargout | Variable-length output argument list |
nargout | Number of function output arguments |
nargoutchk | Validate number of output arguments |
Additional Checks
validateattributes | Check validity of array |
validatestring | Check validity of text |
validatecolor | Validate color values (Since R2020b) |
inputname | Variable name of function input |
mfilename | Filename of currently running code |
inputParser | Input parser for functions |
Topics
Argument Validation
- Function Argument Validation
Declare argument class and size, and enforce restrictions on argument values.
- Validate Required and Optional Positional Arguments
- Validate Repeating Arguments
- Validate Name-Value Arguments
- Argument Validation Functions
Validate specific requirements of arguments using validation functions. Write your own functions to check for specific argument requirements.
- Use Validation Functions to Avoid Unwanted Class and Size Conversions
- Use nargin Functions During Argument Validation
- Check Function Inputs with validateattributes
This example shows how to verify that the inputs toyour function conform to a set of requirements using thevalidateattributes
function. - Parse Function Inputs
Define required and optional inputs, assign defaultsto optional inputs, and validate all inputs to a custom function usingthe Input Parser.
- Input Parser Validation Functions
Number of Arguments
- Support Variable Number of Inputs
Define a function that accepts a variable number ofinput arguments usingvarargin
. Thevarargin
argumentis a cell array that contains the function inputs, where each inputis in its own cell. - Support Variable Number of Outputs
Define a function that returns a variable number ofoutput arguments usingvarargout
. Outputvarargout
isa cell array that contains the function outputs, where each outputis in its own cell. - Find Number of Function Arguments
Use
nargin
andnargout
todetermine how many input or output arguments your function receives.- Validate Number of Function Arguments
- Checking Number of Arguments in Nested Functions
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.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office