mustBeMember

Checks that a value is one of the elements of a set.

Category: Programming > Functions

Syntax

mustBeMember(A, S)

Checks that A is a member of the set S. Returns nothing if the check passes and throws an error if not.

Description

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

The test is all(ismember(A, S)). On failure the message prints the whole set, one value per line, which makes it easy to see the permitted choices.

Input arguments

A
The value to check. Any array of the class the validator accepts.
S
The allowed values: a numeric array, a character vector or a cell array of character vectors.

Example

Passing and failing input

mustBeMember('b', {'a', 'b'});
disp('passed')
try
    mustBeMember(4, [1 2 3])
catch err
    disp(err.message)
end
Output
passed
Value must be a member of this set:
    1
    2
    3

See also

ismember validatestring mustBeInRange mustBeA