MATLAB: Comparing two structures and find the common

structures

I need to compare two strctures and find the common between them and then keep the common fields as the new structure.

Best Answer

common_fields = intersect( fieldnames(structure1), fieldnames(structure2));
for fn = common_fields
this = fn{1};
new_struct(1).(this) = structure1.(this);
new_struct(2).(this) = structure2.(this);
end