Argument Definitions - MATLAB & Simulink - MathWorks Switzerland (2024)

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

Arguments Block

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)
mustBeRowValidate that value is row vector (Since R2024b)
mustBeColumnValidate that value is column vector (Since R2024b)
mustBeMatrixValidate that value is matrix (Since R2024b)

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

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
mfilenameFilename of currently running code
inputParserInput 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 the validateattributes 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 using varargin. The varargin 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 using varargout. Output varargout isa cell array that contains the function outputs, where each outputis in its own cell.
  • Find Number of Function Arguments

    Use nargin and nargout 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.

Argument Definitions- MATLAB & Simulink- MathWorks Switzerland (1)

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)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Argument Definitions
- MATLAB & Simulink
- MathWorks Switzerland (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5716

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.