Argument Definitions - MATLAB & Simulink (original) (raw)

Main Content

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

Comparisons

Data Types

Size

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

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

Topics

Argument Validation

Number of Arguments

Pass-Through Inputs