Compare Two Structures

Compares two structures for equality
Updated 13 Sep 2013

View License

STRUCTCMP(A,B) returns logical 1 (TRUE) if structure A and B are the same
size, contain the same field names (case sensitive), same field sorting, and same values;
and logical 0 (FALSE) otherwise.

If A is defined and you set B = A, STRUCTCMP(A,B) is not necessarily
true. If A's or B's field contains a NaN (Not a Number) element, STRUCTCMP returns
false because NaNs are not equal to each other by definition. To considers NaN values to be equal
use STRUCTCMP(A,B, 'EqualNans', 'on')

The order in which the fields of each structure were created is important. To ignore the field
sorting use STRUCTCMP(A,B, 'IgnoreSorting', 'on')

Field names comparison is case sensitive. To ignore any differences in letter case use
STRUCTCMP(A,B, 'IgnoreCase', 'on')

STRUCTCMP(A,B, 'Report', 'on') displays a report on the command window

Cite As

Javier Lopez-Calderon (2024). Compare Two Structures (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

- Added error message when any input is not a structure.
- When report is 'on' the checking loop continues until the last field (does not use "break").