mustBeInRange

Checks that every element lies between two bounds.

Category: Programming > Functions

Syntax

mustBeInRange(A, lower, upper)

Checks that every element of A lies within the interval from lower to upper. Returns nothing if the check passes and throws an error if not.

mustBeInRange(A, lower, upper, boundflag)

Excludes the lower bound, the upper bound or both from the range, according to boundflag.

Description

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

By default both bounds are allowed, so the range is the closed interval. The message tells which side is open, for example that values must be greater than or equal to the lower bound and less than the upper one.

Input arguments

A
The value to check. Any array of the class the validator accepts.
lower
Lower bound, a real scalar.
upper
Upper bound, a real scalar not smaller than lower.
boundflag
Optional: 'exclusive', 'exclude-lower' or 'exclude-upper' to leave one or both bounds out of the allowed range.

Example

Passing and failing input

mustBeInRange([1 3 5], 1, 5);
disp('passed')
try
    mustBeInRange(7, 1, 5)
catch err
    disp(err.message)
end
Output
passed
Value must be greater than or equal to 1, and less than or equal to 5.

See also

mustBeGreaterThan mustBeLessThan mustBeNonnegative mustBeMember