A struct
in MATLAB is not technically an "object" in the sense that you're talking about it. If I create a struct and then assign it as a field within another struct, the two are now uncoupled. Any changes made to the first struct will not be reflected in the copy we just made.
a = struct('a', 2);
b = struct('b', a);
a.a = 3
% b.b.a == 2
You can really only reliably check that the values of two struct
s are equal.
If you actually want to verify that the two struct
s that you're comparing were created in the same way, you could go through the struct
recursively and determine whether the memory location of each element is the same in both structs. This would imply that the struct is both equal and they were created with the same underlying data.
For a very simple non-deeply nested struct this could look something like this.
function bool = isSameStruct(A, B)
fmt = get(0, 'Format');
format debug;
memoryLocation = @(x)regexp(evalc('disp(x)'), '(?<=pr\s*=\s*)[a-z0-9]*', 'match');
if isequaln(A, B)
bool = true;
elseif ~isequal(sort(fieldnames(A)), sort(fieldnames(B)))
bool = false;
else
fields = fieldnames(A);
bool = true;
for k = 1:numel(fields)
if ~isequal(memoryLocation(A.(fields{k})), memoryLocation(B.(fields{k})))
bool = false;
break;
end
end
end
format(fmt);
end
Update
An alternative is to use actual handle
objects for your nodes. A basic class would look like this.
classdef Node < handle
properties
Value
Children
end
methods
function self = Node(value)
self.Value = value;
end
function addChild(self, node)
self.Children = cat(2, self.Children, node)
end
end
end