Main Content

matlab.unittest.constraints.PublicPropertyComparator class

Package: matlab.unittest.constraints

Comparator for public properties of MATLAB objects


The PublicPropertyComparator compares public properties of MATLAB® objects.

The PublicPropertyComparator supports MATLAB objects or arrays of objects and recursively compares data structures contained in the public properties. The PublicPropertyComparator is different from the isequal function because it examines only the public properties of the objects.

Typically you construct a PublicPropertyComparator using the supportingAllValues static method.


PublicPropertyComparator creates a comparator for public properties of MATLAB objects. This comparator supports only objects with no public properties.

PublicPropertyComparator(compObj) indicates a comparator, compObj, that defines the comparator used to compare public properties. This comparator supports recursion only in the data types supported by compObj.

PublicPropertyComparator(compObj,Name,Value) provides a comparator with additional options specified by one or more Name,Value pair arguments.


supportingAllValuesComparator for public properties that supports any value in recursion

Input Arguments

expand all


Comparator object

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Properties to ignore during object comparison, specified as a cell array of character vectors or a string array.

Example: PublicPropertyComparator('IgnoringProperties',{'Stack'})

Setting for whether 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 public property comparator supports are fully supported in recursion.



Properties to ignore during object comparison, specified in the name-value pair argument, 'IgnoringProperties'.


Indicator of whether comparator operates recursively, specified in the name-value pair argument, 'Recursively'.


  • The PublicPropertyComparator does not compare public properties of objects that overload the subsref, numel, or properties functions.

Copy Semantics

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


collapse all

In a file in your working folder, construct this Employee class.

classdef Employee
    properties (SetAccess=immutable)
    properties (Access=private)
        function obj = Employee(name,location)
            obj.Name = name;
            obj.Location = location;

At the command prompt, create two instances of the Employee class.

e1 = Employee('sam','Building A');
e2 = Employee('Sam','Building B');

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.PublicPropertyComparator
import matlab.unittest.constraints.StringComparator

testCase = TestCase.forInteractiveUse;

Construct a comparator and verify that e1 and e2 are equal.

compObj = PublicPropertyComparator;
testCase.verifyThat(e1, IsEqualTo(e2,'Using',compObj))
Error using matlab.unittest.constraints.Comparator/throwUnsupportedValue (line 313)
None of the currently available comparators support the value.

Available Comparators:
    1×0 Comparator array with no properties.

Value (char):

Error in matlab.unittest.constraints.Comparator>getActExpCompFrom (line 402)

Error in matlab.unittest.constraints.Comparator>deepComparisonIsSatisfied (line 351)
[actVal,expVal,comp] = getActExpCompFrom(comparison);

Error in matlab.unittest.constraints.Comparator>deepComparisonIsSatisfied (line 355)
        if ~deepComparisonIsSatisfied(subComparisonArray(k))

Error in matlab.unittest.constraints.Comparator/satisfiedBy (line 84)
            bool = deepComparisonIsSatisfied(comparison);

Error in matlab.unittest.constraints.IsEqualTo/satisfiedBy (line 193)
            bool = constraint.Comparator.satisfiedBy(actual,constraint.Expected);

Error in matlab.unittest.internal.qualifications.QualificationDelegate/qualifyThat (line 80)
                result = constraint.satisfiedBy(actual);

Error in matlab.unittest.qualifications.Verifiable/verifyThat (line 230)
            qualifyThat(verifiable.VerificationDelegate, ...

The test fails because, by default, the PublicPropertyComparator does not support character vectors.

Construct a comparator that supports character vectors. Specify that the comparison is not case-sensitive.

compObj = PublicPropertyComparator(StringComparator);
testCase.verifyThat(e1, IsEqualTo(e2,'Using',compObj, 'IgnoringCase',true))
Interactive verification passed.

The test passes even though e1.Location and e2.Location are not the same. Since Location is a private property, the comparator does not compare its contents.

Version History

Introduced in R2014a