Main Content

matlab.unittest.constraints.ObjectComparator class

Package: matlab.unittest.constraints

Comparator for MATLAB or Java objects


ObjectComparator creates a comparator for MATLAB® or Java® objects. The comparator is satisfied if isequaln returns true. However, if the class of the expected value defines an isequal method, whether visible or hidden, but not an isequaln method, the ObjectComparator uses that method for comparison instead of isequaln.

ObjectComparator('Within',tolObj) creates a comparator using a specified tolerance. ObjectComparator first checks that a call to isequaln or isequal returns true. If the check fails, the ObjectComparator checks for equivalent class, size, and sparsity of the actual and expected values. If these checks pass, ObjectComparator delegates comparison to the supplied tolerance, tolObj. The value of this tolerance must be of the same class as the actual and expected values.

Input Arguments


Tolerance instance



Specific tolerance used in construction of the comparator, specified as a matlab.unittest.constraints.Tolerance object in the tolObj input argument

Copy Semantics

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


collapse all

In a file, MyInt.m, in your working folder, create a subclass of int8.

classdef MyInt < int8
        function i = MyInt(value)

At the command prompt, create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.ObjectComparator
import matlab.unittest.constraints.IsEqualTo

testCase = TestCase.forInteractiveUse;

Use an ObjectComparator to test that two instances of MyInt are equal to each other.

testCase.verifyThat(MyInt(10), ...
    IsEqualTo(MyInt(10), 'Using', ObjectComparator))
Interactive verification passed.

Test the equality of two instances of MyInt that are constructed with different input values.

testCase.verifyThat(MyInt(11), ...
    IsEqualTo(MyInt(10), 'Using', ObjectComparator))
Interactive verification failed.

Framework Diagnostic:
IsEqualTo failed.
--> ObjectComparator failed.
    --> The objects are not equal using "isequal".

Actual Object:
      int8 data:
Expected Object:
      int8 data:

One instance of MyInt has a value of 11, and the other has a value of 10.

Repeat the test and specify that values must be equal within an absolute tolerance of 1.

import matlab.unittest.constraints.AbsoluteTolerance

testCase.verifyThat(MyInt(11), IsEqualTo(MyInt(10), ...
    'Using', ObjectComparator('Within', AbsoluteTolerance(MyInt(1)))))
Interactive verification passed.


  • In most cases, you are not required to use an ObjectComparator instance. The IsEqualTo class creates a constraint to test for equality between data of various types, including MATLAB and Java objects.

    Use an ObjectComparator instance when you need to override the comparison performed by the IsEqualTo class. For example, if you want the comparison to fail when actual and expected values are not MATLAB objects, include an ObjectComparator instance in your test. In this example, MATLAB throws an exception because the actual and expected values are numeric scalars.

    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.ObjectComparator
    exp = 5; 
    act = exp;
    testCase = matlab.unittest.TestCase.forInteractiveUse;

Version History

Introduced in R2013a