Main Content

matlab.unittest.constraints.HasNaN Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if array has NaN values

Description

The matlab.unittest.constraints.HasNaN class provides a constraint to test if an array has NaN values.

Creation

Description

c = matlab.unittest.constraints.HasNaN creates a constraint to test if an array has NaN values. The constraint is satisfied by a numeric array that has at least one NaN value.

example

Examples

collapse all

Test numeric arrays using the HasNaN constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasNaN

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that NaN satisfies the HasNaN constraint.

testCase.verifyThat(NaN,HasNaN)
Verification passed.

Test if the vector [1 1 2 3 5 8 13] has NaN values. The test fails.

testCase.verifyThat([1 1 2 3 5 8 13],HasNaN)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasNaN failed.
    --> At least one element must be NaN.
    
    Actual Value:
         1     1     2     3     5     8    13

Test [-Inf 5 NaN]. The test passes because the vector contains a NaN value.

testCase.verifyThat([-Inf 5 NaN],HasNaN)
Verification passed.

Test if a complex number whose imaginary part is NaN satisfies the constraint. The test passes.

testCase.verifyThat(3+1i*NaN,HasNaN)
Verification passed.

Test if the matrix [1 NaN; -Inf 3] does not have any NaN values. The test fails.

testCase.verifyThat([1 NaN; -Inf 3],~HasNaN)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Negated HasNaN failed.
    --> All elements must be non-NaN.
        Failing indices:
            3
    
    Actual Value:
         1   NaN
      -Inf     3

Version History

Introduced in R2013a