Contenuto principale

Definizioni degli argomenti

Accettare un numero variabile di input o output, verificare che i valori siano validi

La maggior parte delle funzioni non richiede dichiarazioni o validazioni degli argomenti poiché MATLAB® è un linguaggio non tipizzato. Tuttavia, se la funzione è molto utilizzata e occorre verificare il tipo, la dimensione o altri aspetti degli input per garantire che il codice funzioni come previsto, è possibile definire un blocco 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

Funzioni

espandi tutto

Blocco degli argomenti

argumentsDeclare function argument validation

Validazione dei valori numerici

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

Confronti

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

Tipi di dati

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

Dimensione

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

Appartenenza all’intervallo o all’insieme

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

Nomi

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

Struttura degli argomenti nome-valore

namedargs2cellConvert structure containing name-value pairs to cell array

Input

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

Output

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

Argomenti

Validazione dell’argomento

Numero di argomenti

Input passanti

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