mustBeScalarOrEmpty
Checks that a value is a single element or has none.
Category: Programming > Functions
Syntax
mustBeScalarOrEmpty(A)
Checks that A is a scalar or is empty. Returns nothing if the check passes and throws an error if not.
Description
mustBeScalarOrEmpty is a validator: it inspects its input and either returns silently or raises an error with the identifier MATLAB:validators:mustBeScalarOrEmpty. The error message states the requirement that was violated, so the caller sees what was expected rather than a generic failure.
Useful for optional arguments, where [] means 'not provided' and a scalar means 'use this value'.
Input arguments
A- The value to check. Any array of the class the validator accepts.
Example
Passing and failing input
mustBeScalarOrEmpty(5);
disp('passed')
try
mustBeScalarOrEmpty([1 2])
catch err
disp(err.message)
end
Output
passed
Value must be scalar or empty.