Argument Validation Functions
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.
Numeric Value Attributes
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
Comparison with Other Values
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
|
| |
|
| |
|
|
Data Types
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| Uses class definition relationships |
|
| |
|
| |
|
| |
|
|
Size
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
| value must be a scalar or be
empty. | |
| value must be a vector. | |
| value must be a 1-by-N row
vector. | |
| value must be an M-by-1 column
vector. | |
| value must be an M-by-N
matrix. |
Membership and Range
Name | Meaning | Functions Called on Inputs |
---|---|---|
|
| |
| value must be within range. |
Text
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. |
Define Validation Functions
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 argument. If the function accepts more than one 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.
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.'; error(eidType,msgType) end end
If the 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.