No BSD License  

Highlights from
comparedata

4.8

4.8 | 5 ratings Rate this file 8 Downloads (last 30 days) File Size: 11.83 KB File ID: #1459

comparedata

by Andrew Diamond

 

05 Mar 2002

Compares two objects recursively.

| Watch this File

File Information
Description

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 =

     1

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 =

     0

MATLAB release MATLAB 6.1 (R12.1)
Other requirements Seems to work under 6.0 and 5.3 also.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
23 Aug 2004 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.

23 Aug 2005 Alex Bar-Guy

Excelent and very useful work!!!
Small suggestion - change Line 176 to:
diff=double(data1(:))-double(data2(:));
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.
Thanks

26 Aug 2008 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.

12 Aug 2010 Ged Ridgway

Extremely useful -- thank you!

30 Jun 2011 Mario  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
structures Andrew Diamond 22 Oct 2008 06:42:47
cell arrays Andrew Diamond 22 Oct 2008 06:42:47
compare Andrew Diamond 22 Oct 2008 06:42:47
isequal Andrew Diamond 22 Oct 2008 06:42:47
difference Andrew Diamond 22 Oct 2008 06:42:47
utilities Andrew Diamond 22 Oct 2008 06:42:47

Contact us at files@mathworks.com