Compare Structures

Compares two structured variables recursively and notes where the two structures are different.
Updated 30 Aug 2015

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)

Cite As

michael arant (2024). Compare Structures (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Structures in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

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

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

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