validateattributes

Checks that a value has the required class and properties, raising an error otherwise.

Category: Language Fundamentals > Data Types

Syntax

validateattributes(A, classes, attributes)

Checks that A belongs to one of classes and satisfies every attribute.

validateattributes(A, classes, attributes, argIndex)

Mentions the argument position in the error message.

Description

validateattributes is used at the start of functions to check arguments in one line instead of writing several if and error statements. When a requirement fails it throws an error whose message names the failed attribute. When everything is valid it does nothing and returns no value.

For new code, function argument blocks (arguments) cover the same checks declaratively.

Input arguments

A
The value to test.
classes
Cell array of allowed class names such as {'numeric'}, {'char','string'}; {} allows any class.
attributes
Cell array of requirements such as 'positive', 'integer', 'scalar', 'vector', 'nonempty', 'size'.

Example

A valid value passes silently

validateattributes(5, {'numeric'}, {'positive','integer'});
disp('ok')
Output
ok

Notes

In MatLivre validateattributes accepts its arguments but performs no checks and never raises an error.

See also

validatestring inputParser assert isnumeric narginchk