File Exchange

image thumbnail


version 1.0 (11.8 KB) by

Compares two objects recursively.



No License

Compares two objects recursively, like isequal, but notes differences. Returns 1 if roughly equal where equal can be relaxed to include just the matching fields of structures (or recursive substructures) and/or numeric differences that are under a specified tolerance (default 1e-10). Intermediate comparison results can be printed to file and/or screen.

Help is at top of file.

Note, uses MATLAB's "isequal" function at any recursion level whose object is not a cell, struct, or numeric array.

Example 1: comparison of two simple structures noting but not taking into account fields that are unique to one of the objects.:
>> s1.a=1; s1.b=2; s1.c=3; s2.a=1; s2.b=2;
>> retval=comparedata(s1, s2, [], struct('ignoreunmatchedfieldnames', 1))

context = Top
Mismatch in Top.c not found in second structure
context = Top.a
context = Top.b
retval = 1

Example 2: Comparing nested structures with array elements that are only roughly equal but good enough for the specified tolerance of 0.001. Results printed to screen and file comparedata.out.
>> P1 = struct('s1', struct('s11',[1,2;3,4]));
>> P2 = struct('s1', struct('s11',[1,2;3,4+1e-9]));
>> comparedataParams = struct('NumericTolerance',0.001,'outfileorfid','comparedata.out');
>> retval=comparedata(P1,P2, '',struct('NumericTolerance',0.001,'outfileorfid','comparedata.out'))
context = Top
context = Top.s1
context = Top.s1.s11

retval =


Example 3: Building on example 2 with arrays of nested structures. Parameters set to just print to screen and to use default numeric tolerance of 1e-10.
>> aP1 = [P1,P1]; aP2 = [P2, P2]; % create arrays of structures to compare.
>> retval=comparedata(aP1,aP2)
context = Top
context = Top[1, 1].s1
context = Top[1, 1].s1.s11
Mismatch : at Top[1, 1].s1.s11 numeric array comparison - abs(data 1 - data 2) > tolerance of 1e-010:
    mindiff = -1.000000e-009 at [2, 2]
    maxdiff = 0.000000e+000 at [1, 1]
    max abs diff = 1.000000e-009 at [2, 2]
context = Top[1, 2].s1
context = Top[1, 2].s1.s11
Mismatch : at Top[1, 2].s1.s11 numeric array comparison - abs(data 1 - data 2) > tolerance of 1e-010:
    mindiff = -1.000000e-009 at [2, 2]
    maxdiff = 0.000000e+000 at [1, 1]
    max abs diff = 1.000000e-009 at [2, 2]

retval =


Comments and Ratings (5)


Mario (view profile)

Ged Ridgway

Ged Ridgway (view profile)

Extremely useful -- thank you!

Chris Tilly

An interesting enhancement would be to determine the area(s) within non-identical matrices that have changed, ie. one column, one row, a patch of changes spanning rows/cols, promoting groups as required.

Alex Bar-Guy

Excelent and very useful work!!!
Small suggestion - change Line 176 to:
and line 183 to:
fprintf(ParamsP.fid,'Scalar 1 = %e, Scalar 2 = %e, Scalar1 - Scalar2 = %e\n', double( data1 ), double( data2 ), double(data1)-double(data2));
to support data types different from double.

pete scotson

Did what it said and I found it useful for comparing structures, where isequal just did not do the job because the structures already had known differences. In my case I had two nominally identical structures generated by different routines, and I wanted a check that they produced the same answer for all but the source field (which was different by definition). comparedata took me striaght to the differences, and I could then fix them. Thanks!

Not sure what the "context" argument was for, but then I did not read the help file very thoroughly - comparedata did enough using the defaults.

MATLAB Release
MATLAB 6.1 (R12.1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video