Definition von Argumenten
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, können Sie einen arguments
-Block definieren (seit R2019b). Weitere Informationen finden Sie unter Function Argument Validation.
Zusätzlich können Sie prüfen, wie viele Eingaben oder Ausgaben Ihre Funktion empfängt, die Namen der Eingabevariablen und mehr.
Funktionen
Themen
Validierung von Argumenten
- Function Argument Validation
Declare argument class and size, and enforce restrictions on argument values.
- Argument Validation Functions
Validate specific requirements of arguments using validation functions. Write your own functions to check for specific argument requirements.
- Check Function Inputs with validateattributes
This example shows how to verify that the inputs to your function conform to a set of requirements using thevalidateattributes
function. - Parse Function Inputs
Define required and optional inputs, assign defaults to optional inputs, and validate all inputs to a custom function using the Input Parser.
Anzahl von Argumenten
- Support Variable Number of Inputs
Define a function that accepts a variable number of input arguments usingvarargin
. Thevarargin
argument is a cell array that contains the function inputs, where each input is in its own cell. - Support Variable Number of Outputs
Define a function that returns a variable number of output arguments usingvarargout
. Outputvarargout
is a cell array that contains the function outputs, where each output is in its own cell. - Find Number of Function Arguments
Use
nargin
andnargout
to determine how many input or output arguments your function receives.
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.