View License

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

» Watch video

Highlights from
Compare Structures

4.6 | 31 ratings Rate this file 47 Downloads (last 30 days) File Size: 6.6 KB File ID: #22752 Version: 2.0

Compare Structures


michael arant (view profile)


21 Jan 2009 (Updated )

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

| Watch this File

File Information

This is a completely new version of the tool with new capabilities based on comments friend gave me the on how to make this more useful. The suggestion was have the app report the common content between two structures and the unique content of each structure in structure form - essentially, parsing the input structures. The list output is now executed using a separate app (list_sruct). So to compare two structures use comp_struct. To expand (unpack) the structures, use list_struct on each output. This should also go a long way to addressing this issues noted by Brad Stiritz, David Groppe, and David Provencher.
Older code included as a reference if needed.
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)

MATLAB release MATLAB 7.14 (R2012a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (35)
20 Oct 2016 Ken Purchase

Very useful! I found it valuable to list the full differences using list_struct(m,1) but I had to hack it to suppress displays of large matrices. I changed line 56 to:
(in this case a hack only good for matrices up to 4 dimensions)

07 Jul 2016 Aloïs Wolff

03 May 2016 Mdaatniel

Sorry for the bogus 0/5 rating, please, someone, cancel that 0/5 rating.

comp_struct Works very well. Typo "parrent" in a comment.

I prefer to have printf go in common, so that I replaced "common = [];" by "common = fprintf();", and I added following lines on bottom:

function res=fprintf(varargin)
persistent errormessage
if nargin>0;

03 May 2016 Mdaatniel

Sorry for the bogus 0/5 rating, please, someone, cancel that rating 0/5.

03 May 2016 Mdaatniel

17 Mar 2016 majid sadeghi

Thank you so much.

18 Jul 2015 Matthew Phillips

Thanks bro this saved me time.

05 Jul 2015 Henrik

Henrik (view profile)

15 Apr 2015 Stefan Grandl

03 Apr 2015 Emily Yunha Shin

03 Sep 2014 Alexandre Tumlinson

Saved me a lot of time, thanks.

03 Sep 2014 Alexandre Tumlinson

I like it,

I made a small change at line 196 so the waitbars indicate where you are in the process.

waitmsg={['Comparing ' n1 ' and ' n2 '....'];'[spacebar] to step if paused'};
waitmsg=['Comparing ' n1 ' and ' n2 '....'];
if wbf; wb = waitbar(0,waitmsg); end

Would be great if waitbars appeared staggered on screen. Currently depth in structure does not seem to be passed explicitly. I think you could extract it from the name syntax?

Comment only
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

michael arant (view profile)

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

Comment only
29 Apr 2014 Tommy

Tommy (view profile)

Nice tool!

I would recommend changing the line
er = norm(s1-s2);
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.


23 Nov 2013 Pedro Dreyer

This saved me a lot of time. Thank you.

25 Sep 2013 Austin

Austin (view profile)

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};

25 Apr 2013 michael arant

michael arant (view profile)

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

Comment only
25 Apr 2013 Brian

Brian (view profile)

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

Martin (view profile)

12 Mar 2013 Martin

Martin (view profile)

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 Jdh

Jdh (view profile)

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

michael arant (view profile)

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.

Comment only
04 Feb 2010 David Groppe

David Groppe (view profile)

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

Roland Pfister (view profile)

Yeah - very useful. Thanks a lot!

26 Aug 2009 Bass

Bass (view profile)

11 Mar 2009 Shadi

Shadi (view profile)

Works great for me! Thanks

29 Apr 2013 1.1

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 1.3

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

30 Aug 2015 2.0

New version where results are reported as structures (matches, err1, err2). Results can be expanded with list_struct.

Contact us