Main Content

matlab.unittest.constraints.IsReal Class

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

Test if array elements are real values

Description

The matlab.unittest.constraints.IsReal class provides a constraint to test if elements of an array are real values.

Creation

Description

example

c = matlab.unittest.constraints.IsReal creates a constraint to test if all elements of an array are real values. The constraint is satisfied by a numeric array that does not use complex storage.

Examples

collapse all

Test numeric arrays using the IsReal constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsReal

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the value 5 is real.

testCase.verifyThat(5,IsReal)
Verification passed.

Now test the value complex(5). Even though the value of the imaginary part is zero, the test fails because the value is stored as a complex number.

testCase.verifyThat(complex(5),IsReal)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsReal failed.
    --> The value must be real.
    
    Actual Value:
      5.000000000000000 + 0.000000000000000i

Test the value sqrt(-1). The test fails.

testCase.verifyThat(sqrt(-1),IsReal)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsReal failed.
    --> The value must be real.
    
    Actual Value:
      0.000000000000000 + 1.000000000000000i

Test if the vector [1 1 2 3 5 8 13] satisfies the IsReal constraint. The test passes because all vector elements are real.

testCase.verifyThat([1 1 2 3 5 8 13],IsReal)
Verification passed.

Test a vector that contains infinite and NaN values. The test passes.

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

Verify that the matrix [1 NaN; -Inf 3i] is not real.

testCase.verifyThat([1 NaN; -Inf 3i],~IsReal)
Verification passed.

Version History

Introduced in R2013a