mustBeNonempty

Checks that an array has at least one element.

Category: Programming > Functions

Syntax

mustBeNonempty(A)

Checks that A is not empty. Returns nothing if the check passes and throws an error if not.

Description

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

An array is empty when any of its dimensions is zero, so zeros(0, 3) and '' both fail.

Input arguments

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

Example

Passing and failing input

mustBeNonempty([1 2]);
disp('passed')
try
    mustBeNonempty([])
catch err
    disp(err.message)
end
Output
passed
Value must not be empty.

See also

isempty mustBeNonzeroLengthText mustBeScalarOrEmpty mustBeVector