Main Content

matlab.unittest.constraints.HasLength class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Constraint specifying expected length of array


HasLength(lengthVal) provides a constraint that specifies an expected length of an array. The constraint is satisfied if the largest dimension length of the actual value array has the same number of elements specified as by lengthVal.

Input Arguments


Length a value must have to satisfy the constraint.



Length a value must have to satisfy the constraint. Set this property through the constructor via the lengthVal input argument.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.


collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasLength

testCase = TestCase.forInteractiveUse;

Assert that a 2x5x3 array has an expected length.

testCase.assertThat(rand(2, 5, 3), HasLength(5))
Interactive assertion passed.

Verify that a cell array of character vectors has an expected length.

testCase.verifyThat({'SomeString', 'SomeOtherString'}, HasLength(2))
Interactive verification passed.

Verify that an identity matrix has an expected length.

testCase.verifyThat(eye(2), HasLength(4))
Interactive verification failed.

Framework Diagnostic:
HasLength failed.
--> The array has an incorrect length.
    Actual Length:
    Expected Length:

Actual Array:
         1     0
         0     1

The matrix has a length of 2.