find the difference between two structures in matlab

One way to find the difference between two structures is to use the isequal function. Here's an example:

main.m
% define the structures
s1.field1 = 1;
s1.field2 = 'hello';
s1.field3 = [2 3];

s2.field1 = 1;
s2.field2 = 'world';
s2.field3 = [2 3];

% compare the structures
isequal(s1, s2)
177 chars
12 lines

The output will be false because the values of the field2 fields are different. If you want to see which fields are different, you can use the structdiff function from the File Exchange:

main.m
% install structdiff from the File Exchange
addpath('structdiff')

% compare the structures
structdiff(s1, s2)
111 chars
6 lines

This will output a struct that lists the fields that are different between the two input structs.

related categories

gistlibby LogSnag