Code covered by the BSD License  

Highlights from
Compare Structures

4.42105

4.4 | 20 ratings Rate this file 79 Downloads (last 30 days) File Size: 10.3 KB File ID: #22752

Compare Structures

by

 

21 Jan 2009 (Updated )

Compares two structured variables recursively and notes where the two structures are different.

| Watch this File

File Information
Description

Oct 19, 2013

This is an update of the code to provide better lists of the errors encountered. The clear limitation of the prior re-write was in post processing the data (thanks to Brad Stiritz and Brett Shoelson for useful comments on how to address the issue). The new code is essentially the same algorithm (some minor changes to how errors were documented) but with a new error evaluation algorithm added. The great difficulty here is in managing the potential combinations of structure mismatches. For instance, the code could encounter issues such as:
a.a is class double, b.a is class char
a.b exists, b.b does not exist
a(2) exists, b(2) does not exist
a.c and b.c are both the same class but have different contents
a.d and b.d are both functions (even the same function) but are evaluated at different points
a.e is a substructure while b.e is a double, char, cell, ….

The new output attempts to manage all of these combinations by developing a cell with N rows (for N errors) and 3 columns (error label, structure 1 contents, and structure contents).

NOTE: The calling syntax has changed (new output variables) so it is not a direct update of the prior code. You will need to update the calling functions if you are currently using the prior script.

_______________________________________
Apr 29, 2013
This is a scratch re-write of the code taking advantage of new abilities in MATLAB since I wrote the first version 10 years ago. Original version was just something I wrote in desperation and was not very elegant. This version should be more usable. Original version is included for posterity.

Calling syntax is similar to the original but now you can:
1. Disable all printouts to screen, print out only missing fields, print out all errors, or print out all errors and matches.
2. Activate a waitbar for progress (use if you are not printing out errors and your structures are large)
3. Collect all errors into a single cell (rather than two cells for each structure)
4. Structure order does not matter. Tool looks for matching fields.

Thanks to Brian for suggestions and a validation test. Thanks to David Groppe for the error only output suggestion. Thanks to David Provencher for the output only suggestion. Thanks to Keith Beardmore for pointing out the sort order issue.
_______________________________________
Original post:

Structured variables are good for organizing data and manipulating data. However, they can be difficult to check for errors or differences. Function allows each field of a structure to be checked against a corresponding field in a separate structure. Possible outcomes are:
1. Fields agree (both exist and have the same content)
2. Fields do not agree (both exist but contents / values are different)
3. Field exists in only one structure
4. Field type differs (variable class disagreement)

Acknowledgements

This file inspired Mass Univariate Erp Toolbox.

MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (23)
01 May 2014 Christopher

And now that I've opened the function for the first time, I see it's done already.

That's what I get for posting in a hurry.

30 Apr 2014 michael arant

Thanks Tommy. Actually, an earlier version did have a division like that. But when you need to compare 0 to 0 (or numerical noise), it breaks down...

29 Apr 2014 Tommy

Nice tool!

I would recommend changing the line
er = norm(s1-s2);
to
er = norm(s1./s2-1);

If I am comparing a struct, simply containing parameter values, it makes sense to use the relative error.

E.g. comparing 5e-12 with 4e-12 should not be a tolerance match.

Regards,
Tommy

23 Nov 2013 Pedro Dreyer

This saved me a lot of time. Thank you.

25 Sep 2013 Austin

This was very helpful. Thank you for your contribution.

21 Jul 2013 Brad Stiritz

Very useful function, but message output is not conducive to machine-processing. It would be helpful to have an option to return only the non-matching field names per se.

I didn't choose to modify the function code, but instead wrote a helper function which extracts the field names from the output messages. Here's the core logic, hope it may be helpful to someone..

%{
Here's an example of the message output from comp_struct():

>>st_1.NAME = 'abc';
>>st_2.NAME = 'def';
>>[~,cv_st_1_msgs] = comp_struct(st_1,st_2,0,0,0,'','',0)
cv_st_1_msgs = '(1).NAME and (1).NAME do not match'

We want to extract 'NAME' from this string. Using free utility Expresso, I
developed a regular expression to capture the first occurence of 'NAME'.
%}

% Declare regular expression to capture field name from comp_struct()
% message output:
regex = '\(.*\)\.(.*)\sand';

% Apply (regex) to all comp_struct output strings & return the capture groups as an (N x 1) cell vector of doubly-nested cell vectors containing the token match.
cv_cv_cv_fieldnames = regexp(cv_messages,regex,'tokens');

% Preallocate output:
cv_fieldnames = cell(N_fields,1);

% Iterate through the regexp() output:
for i_field = 1 : N_fields

% Assign (i)'th output element:
cv_fieldnames{i_field} = cv_cv_cv_fieldnames{i_field}{1}{1};
end

25 Apr 2013 michael arant

Brian: You are correct on the function handle issue. I never planned on that type of structure. Seems that this tool is due for an overhaul.....

25 Apr 2013 Brian

Thanks, saved me some time.
It will fail to compare certain types of structure data. If the fields of the structure contain function handles, or various types of simulink objects. The failure generally occurs on line 151, because the subtract operator is not defined for all data types.

22 Mar 2013 David Provencher

Very useful. Would have liked a 'silent' option to disable output to command window and use only the output variables for large structures.

12 Mar 2013 Martin  
12 Mar 2013 Martin

thank you ;-)

15 Nov 2012 Matthew Crema

Great. Like others, this function saved me hours.

04 Oct 2011 Keith Beardmore

Thanks - saved me hours.
My fields were in a different order in my two structures so I had to reorder one: comp_struct(s1,orderfields(s1,s2)).

05 Feb 2011 Benjamin

This script saved me hours of time... thanks

03 Dec 2010 Joost den Haan  
19 Oct 2010 Jaime Undurraga

very nice

09 Sep 2010 Florian Dignath

Thank you very much indeed! * * * * *
Sorry for the disturbed rating, my rating always vanishes the moment I click on submit. Both in MS IE and Firefox.

09 Apr 2010 Christian Komposch  
05 Feb 2010 michael arant

Thank you David:
I’ll keep that in mind for an upgrade. I intended the pause command to be used to flag mismatched cases. That and the “er” output. When I get the chance, I’ll add an additional input to turn off the echo of all the structure fields.

04 Feb 2010 David Groppe

Thanks! Very helpful.
If would be lovely if you could add an option so that the function only displays mismatches. Displaying all the matches is too much information for the application I'm working on.

16 Sep 2009 Roland Pfister

Yeah - very useful. Thanks a lot!

26 Aug 2009 Bass  
11 Mar 2009 Shadi

Works great for me! Thanks

Updates
29 Apr 2013

This is a scratch re-write of the code taking advantage of new abilities in MATLAB. This version should be more usable.

21 Oct 2013

This is an update of the code to provide better lists of the errors encountered.

Contact us