Contenu principal

Définitions d’arguments

Accepter un nombre variable d’entrées ou de sorties et vérifier la validité des valeurs

La plupart des fonctions ne nécessitent pas de déclaration ni de validation d’argument car MATLAB® est un langage non typé. Cependant, si votre fonction est largement utilisée et que vous avez besoin de vérifier le type, la taille ou d’autres aspects des entrées pour vous assurer que votre code fonctionne comme prévu, vous pouvez définir un bloc arguments.

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

Fonctions

développer tout

Bloc arguments

argumentsDeclare function argument validation

Validation des valeurs numériques

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 input contains NaN
mustBeNonzeroValidate that value is nonzero
mustBeNonsparseValidate that value is nonsparse
mustBeSparseValidate that value is sparse (depuis R2023b)
mustBeRealValidate that value is real
mustBeIntegerValidate that value is integer
mustBeNonmissingValidate that input does not contain missing values

Comparaisons

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

Types de données

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

Taille

mustBeNonemptyValidate that value is nonempty
mustBeScalarOrEmptyValidate that value is scalar or empty
mustBeVectorValidate that value is vector
mustBeRowValidate that value is row vector (depuis R2024b)
mustBeColumnValidate that value is column vector (depuis R2024b)
mustBeMatrixValidate that value is matrix (depuis R2024b)

Plage de valeurs et appartenance à un ensemble

mustBeBetweenValidate that all elements are within specified range (depuis R2025a)
mustBeInRangeValidate that value is in the specified range
mustBeMemberValidate that value is member of specified set

Noms

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

Structure d’arguments nom-valeur

namedargs2cellConvert structure containing name-value pairs to cell array

Entrées

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

Sorties

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
inputnameVariable name of function input
mfilenameFilename of currently running code
inputParserInput parser for functions

Rubriques

Validation des arguments

Nombre d’arguments

Ignorer des entrées

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