MATLAB defines functions for use in argument validation. These functions support common use patterns for validation and provide descriptive error messages. The following tables categorize the MATLAB® validation functions and describe their use.
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
|
| |
|
| |
|
|
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| Uses class definition relationships |
|
| |
|
| |
|
| |
|
|
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
| value must be a scalar or be
empty. | |
| value must be a vector. |
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
| value must be within range. |
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
| path must refer to a folder. | |
|
| Not applicable |
|
| Not applicable |
|
| Not applicable |
| varname must be a valid variable
name. |
Validation functions are MATLAB functions that check requirements on values entering functions or properties. Validation functions determine when to throw errors and what error messages to display.
Functions used for validation have these design elements:
Validation functions do not return outputs or modify program state. The only purpose is to check the validity of the input value.
Validation functions must accept the value being validated as an input argument. If the function accepts more than one input argument, the first input is the value to be validated.
Validation functions rely only on the inputs. No other values are available to the function.
Validation functions throw an error if the validation fails. Using throwAsCaller
to throw exceptions avoids showing the validation function
itself in the displayed error message.
Creating your own validation function is useful when you want to provide specific validation that is not available using the MATLAB validation functions. You can create a validation function as a local function within the function file or place it on the MATLAB path. To avoid a confluence of error messages, do not use function argument validation within user-defined validation functions.
For example, the mustBeRealUpperTriangular
function restricts the
input to real-valued, upper triangular matrices. The validation function uses the istriu
and isreal
functions.
function mustBeRealUpperTriangular(a) if ~(istriu(a) && isreal(a)) eidType = 'mustBeRealUpperTriangular:notRealUpperTriangular'; msgType = 'Input must be a real-valued, upper triangular matrix.'; throwAsCaller(MException(eidType,msgType)) end end
If the input argument is not of the correct type, the function throws an error.
a = [1 2 3+2i; 0 2 3; 0 0 1]; mustBeRealUpperTriangular(a)
Input must be a real-valued, upper triangular matrix.