I need to check if two MATLAB structs (all fieldnames and values) are equal. Both of these structs are structs of structs. Occasionally, these structs are equal except for one thing: the field values in one struct are the transpose of the field values in the other.
If I use the function isequal to check if the two structs are equal, I get a negative result if the field contents are transposed.
Example:
cfg1.x = {'a' 'b'};
cfg1.y.z = {'c' 'd'};
cfg2.x = {'a' 'b'}';
cfg2.y.z = {'c' 'd'}';
isequal(cfg1, cfg2)
>> isequal(cfg1, cfg2)
ans =
logical
0
One solution is before checking for equality, I could loop over the fields of one struct and make sure the sizing aligns with the fields of the other struct by transposing if necessary. However, this doesn't seem very efficient, and I like to avoid loops when I can. Is there a function similar to isequal that is transpose-invariant?
A while ago I wrote my own recursive comparison of nested structs (mostly to see where variables differ) and you only need to replace
isequal(a,b)withisequal(a,b) || isequal(a,b')in one line in the code to get a transpose-invariant isequal behavior. The code throws an error if two variables are not equal and also says where.