Main Content

matlab.unittest.constraints.HasMissing Class

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

Test if array has missing elements

Since R2023b

Description

The matlab.unittest.constraints.HasMissing class provides a constraint to test if an array has any missing elements. The constraint uses the ismissing function to identify missing values.

The form that missing values take in MATLAB® depends on the data type. For more information, see Missing Data in MATLAB.

Creation

Description

example

c = matlab.unittest.constraints.HasMissing creates a constraint to test if an array has any missing elements. The constraint is satisfied by an array that has at least one missing element.

Examples

collapse all

Test for missing elements using the HasMissing constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasMissing

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Numeric data types, such as double, use NaN to represent missing values. Verify that NaN satisfies the HasMissing constraint.

testCase.verifyThat(NaN,HasMissing)
Verification passed.

Test if the numeric vector [-Inf 0 Inf] has any missing elements. The test fails because none of the elements are missing values.

testCase.verifyThat([-Inf 0 Inf],HasMissing)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasMissing failed.
    --> At least one element must be missing.
    
    Actual Value:
      -Inf     0   Inf

Test if the matrix [missing "a"; "b" "c"] has no missing elements. The test fails because one of the elements is a missing value.

testCase.verifyThat([missing "a"; "b" "c"],~HasMissing)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Negated HasMissing failed.
    --> Value must not contain any missing elements.
        Indices with missing values:
            1
    
    Actual Value:
      2×2 string array
    
        <missing>    "a"
        "b"          "c"

Verify that {''} contains a missing element.

testCase.verifyThat({''},HasMissing)
Verification passed.

Test if a table contains any missing elements by using the HasMissing constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasMissing

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create a table with variables of different data types.

doubleVar = [1; 2; 3; 4; 5; 6];
singleVar = single([1; 2; 3; 4; 5; 6]);
cellstrVar = {'one'; 'two'; ''; 'four'; 'five'; 'six'};
categoricalVar = categorical({'red'; 'orange'; 'yellow'; ''; 'blue'; 'indigo'});
datetimeVar = [datetime(2023,1:6,15)]';
stringVar = ["a"; "b"; "c"; "d"; "e"; "f"];

T = table(doubleVar,singleVar,cellstrVar,categoricalVar,datetimeVar,stringVar)
T=6×6 table
    doubleVar    singleVar    cellstrVar    categoricalVar    datetimeVar    stringVar
    _________    _________    __________    ______________    ___________    _________

        1            1        {'one'   }     red              15-Jan-2023       "a"   
        2            2        {'two'   }     orange           15-Feb-2023       "b"   
        3            3        {0×0 char}     yellow           15-Mar-2023       "c"   
        4            4        {'four'  }     <undefined>      15-Apr-2023       "d"   
        5            5        {'five'  }     blue             15-May-2023       "e"   
        6            6        {'six'   }     indigo           15-Jun-2023       "f"   

Test if the table contains any missing elements. The test passes because at least one element in the table is a missing value. In this example, the third element of cellstrVar is '' and the fourth element of categoricalVar is <undefined>, which are missing values.

testCase.verifyThat(T,HasMissing)
Verification passed.

Version History

Introduced in R2023b