mustBeA
Checks that a value belongs to one of the given classes.
Category: Programming > Functions
Syntax
mustBeA(A, classname)
Checks that A is of the given class or inherits from it. Returns nothing if the check passes and throws an error if not.
Description
mustBeA is a validator: it inspects its input and either returns silently or raises an error with the identifier MATLAB:validators:mustBeA. The error message states the requirement that was violated, so the caller sees what was expected rather than a generic failure.
The test is isa(A, classname), so a subclass of the named class passes as well. With several names, the value is accepted if it is of any of them.
Input arguments
A- The value to check. Any array of the class the validator accepts.
classname- Class name as text, or a cell array or string array of names. The value passes if it matches any of them.
Example
Passing and failing input
mustBeA(5, 'double');
disp('passed')
try
mustBeA(5, 'char')
catch err
disp(err.message)
end
Output
passed
Value must be one of the following types: 'char'.