Matlab arguments validation
Matlab
function arguments validation
syntax is generally recommended over validateattributes()
and inputParser()
.
Function arguments validation specification coerces the input and/or output variables to the class declaration given if possible, and errors otherwise.
- Default values are easily specified, which required such verbose syntax before.
- only a single class can be specified
- recall the .empty method of most Matlab classes e.g.
datetime.empty()
that allows initializing an empty array.
Matlab argument validation syntax coerces class at runtime.
GNU Octave compatibility
Currently GNU Octave does not enable function arguments validation syntax, but it is possible to use the function albeit with warnings like:
“function arguments validation blocks are not supported; INCORRECT RESULTS ARE POSSIBLE”
To elide the verbose warnings to allow for self-tests or quick use, do in Octave like:
warning('off', 'all')
% whatever code you want to run that uses the Matlab arguments validation syntax
oruntests .
% restore warnings
warning('on', 'all')
There are requests for Octave to assign a warning ID so that only this warning can be silenced, but it is not implemented as of this writing. Thus all warnings are silenced, which is not recommended in production code. However, for self-tests or quick use, it could be acceptable.