File Exchange

image thumbnail

Compare Structures

version 2.0 (6.6 KB) by

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

4.60714
31 Ratings

42 Downloads

Updated

View License

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)

Comments and Ratings (35)

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:
disp(s1(1:min(end,10),1:min(end,10),1:min(end,10),1:min(end,10)))
(in this case a hack only good for matrices up to 4 dimensions)

Aloïs Wolff

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;
    errormessage=[{sfprintf(varargin{:})},varargin];
    res=[];
  else
    res=errormessage;
  end

Mdaatniel

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

Mdaatniel

majid sadeghi

Thank you so much.

Thanks bro this saved me time.

Henrik

Henrik (view profile)

Stefan Grandl

Saved me a lot of time, thanks.

I like it,

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

if(pse)
        waitmsg={['Comparing ' n1 ' and ' n2 '....'];'[spacebar] to step if paused'};
    else
        waitmsg=['Comparing ' n1 ' and ' n2 '....'];
    end
    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?

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.

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

Tommy

Tommy (view profile)

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

Pedro Dreyer

This saved me a lot of time. Thank you.

Austin

Austin (view profile)

This was very helpful. Thank you for your contribution.

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

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

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.

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

Martin

Martin (view profile)

Martin

Martin (view profile)

thank you ;-)

Matthew Crema

Great. Like others, this function saved me hours.

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

Benjamin

This script saved me hours of time... thanks

Jdh

Jdh (view profile)

very nice

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.

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.

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.

Roland Pfister

Roland Pfister (view profile)

Yeah - very useful. Thanks a lot!

Bass

Bass (view profile)

Shadi

Shadi (view profile)

Works great for me! Thanks

Updates

2.0

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

1.3

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

1.1

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

MATLAB Release
MATLAB 7.14 (R2012a)

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

» Watch video