mustBeNonnegative

Checks that every element is zero or greater.

Category: Programming > Functions

Syntax

mustBeNonnegative(A)

Checks that every element of A is greater than or equal to zero. Returns nothing if the check passes and throws an error if not.

Description

mustBeNonnegative is a validator: it inspects its input and either returns silently or raises an error with the identifier MATLAB:validators:mustBeNonnegative. The error message states the requirement that was violated, so the caller sees what was expected rather than a generic failure.

Zero passes. Use mustBePositive when zero must be excluded.

Input arguments

A
The value to check. Any array of the class the validator accepts.

Example

Passing and failing input

mustBeNonnegative([0 2 5]);
disp('passed')
try
    mustBeNonnegative(-1)
catch err
    disp(err.message)
end
Output
passed
Value must be nonnegative.

See also

mustBePositive mustBeNonpositive mustBeGreaterThanOrEqual mustBeInteger