Hauptinhalt

Definition von Argumenten

Eine variable Anzahl von Eingaben oder Ausgaben akzeptieren, auf gültige Werte prüfen

Da MATLAB® eine schwach typisierte Sprache ist, erfordern die meisten Funktionen keine Argumentdeklarationen oder -validierung. Wenn Ihre Funktion jedoch breit genutzt wird und Sie den Typ, die Größe oder andere Aspekte von Eingaben überprüfen möchten, um sicherzustellen, dass Ihr Code wie erwartet funktioniert, dann können Sie einen arguments-Block definieren.

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

Funktionen

alle erweitern

Argument-Block

argumentsDeclare function argument validation

Validieren von numerischen Werten

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 (Seit R2023b)
mustBeRealValidate that value is real
mustBeIntegerValidate that value is integer
mustBeNonmissingValidate that input does not contain missing values

Vergleiche

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

Datentypen

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

Größe

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

Bereichs- und Satz-Zugehörigkeit

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

Namen

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

Name-Wert-Argumentstruktur

namedargs2cellConvert structure containing name-value pairs to cell array

Eingaben

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

Ausgaben

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

Themen

Validierung von Argumenten

Anzahl von Argumenten

Ignorierte Eingaben

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