Main Content

matlab.unittest.constraints.TableComparator class

Package: matlab.unittest.constraints

Comparator for MATLAB tables

Construction

TableComparator creates a comparator for MATLAB® tables that iterates over each column of the table. By default, a TableComparator supports only empty tables.

TableComparator(compObj) indicates a comparator, compObj, that defines the comparator used to compare values contained in the table columns.

TableComparator(___,Name,Value) provides a comparator with additional options specified by one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Input Arguments

expand all

Comparator object, specified as an instance of matlab.unittest.constraints that is classified as a comparator.

Example: CellComparator

Example: NumericComparator

Example: StringComparator

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Setting for whether the comparator operates recursively, specified as false or true (logical 0 or 1). When this value is false, the comparator does not operate recursively on its data.

When the value is true, the data types that the table comparator supports are fully supported in recursion.

Data Types: logical

Properties

expand all

Indicator of whether the comparator operates recursively, returned as false or true (logical 0 or 1).

Data Types: logical

Copy Semantics

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

Examples

collapse all

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Create two equal tables.

LastName = {'Williams';'Jones';'Brown'};
Age = [38;40;49];
Height = [64;67;64];
Weight = [131;133;119];
BloodPressure = [125 83; 117 75; 122 80];

T1 = table(Age,Height,Weight,BloodPressure, ...
    'RowNames',LastName);
T2 = T1;

Test that the tables are equal. Check the columns of the tables with a numeric comparator.

import matlab.unittest.constraints.TableComparator
import matlab.unittest.constraints.NumericComparator
import matlab.unittest.constraints.IsEqualTo
testCase.verifyThat(T1,IsEqualTo(T2, ...
    'Using',TableComparator(NumericComparator)))
Interactive verification passed.

Change the age of the last person to 50 and compare the tables again.

T2.Age(end) = 50;
testCase.verifyThat(T1,IsEqualTo(T2, ...
    'Using',TableComparator(NumericComparator)))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> Path to failure: <Value>.Age
    --> NumericComparator failed.
        --> The values are not equal using "isequaln".
        --> Failure table:
            value =
            
              1×5 table array
            
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
            
                3        49        50          -1       -0.02        
        
        Actual double:
                38
                40
                49
        Expected double:
                38
                40
                50

Actual table:
    value =
    
      3×4 table array
    
                    Age    Height    Weight    BloodPressure
                    ___    ______    ______    _____________
    
        Williams    38     64        131       125     83   
        Jones       40     67        133       117     75   
        Brown       49     64        119       122     80   
Expected table:
    value =
    
      3×4 table array
    
                    Age    Height    Weight    BloodPressure
                    ___    ______    ______    _____________
    
        Williams    38     64        131       125     83   
        Jones       40     67        133       117     75   
        Brown       50     64        119       122     80   

Tips

  • In most cases, you are not required to use a TableComparator object. The IsEqualTo class creates a constraint to test for equality between data of various types, including tables.

    Use a TableComparator object when you need to override the comparison performed by the IsEqualTo class. For example, if you want the comparison to fail when tables include nonnumeric values, include a TableComparator object in your test. In this example, MATLAB throws an exception because T1 and T2 contain nonnumeric values.

    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.TableComparator
    import matlab.unittest.constraints.NumericComparator
          
    T1 = table([45;32;34],logical([1;0;0]),'VariableNames',{'Age','Vote'}); 
    T2 = T1;
    
    testCase = matlab.unittest.TestCase.forInteractiveUse;
    testCase.verifyThat(T2,IsEqualTo(T1,'Using',TableComparator(NumericComparator)))
    

Introduced in R2017a