Code covered by the BSD License  

Highlights from
Compare Structures

4.28571

4.3 | 15 ratings Rate this file 101 Downloads (last 30 days) File Size: 6.1 KB File ID: #22752

Compare Structures

by michael arant

 

21 Jan 2009 (Updated 29 Apr 2013)

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

| Watch this File

File Information
Description

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)

______________________________________
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.

Acknowledgements

This file inspired Mass Univariate Erp Toolbox.

MATLAB release MATLAB 7.14 (R2012a)
Tags for This File  
Everyone's Tags
compare(8), equal(4), structure(4), thank you
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
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.

Contact us