Checking if two handle objects are identical using 'isequal' and '=='
9 次查看(过去 30 天)
显示 更早的评论
I am studying object and class in MATLAB and I found something I can't understand.
From this link (https://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html)
I see that "a == b" for handle object "a" and "b" determines if "a" and "b" refere to the same object
and "isequal(a,b)" determines if "a" and "b" have the same values for their attributes.
So for the histogram objects as below, I expected that the results of "a==b" and "isequal(a,b)" should be "0" and "1" respectively,
because they do not refer to the same objects but they have the same values for all attributes.
But my thought was wrong and I don't know why. please help
x = randn(1000,1);
h = histogram(x);
g = histogram(x);
isequal(g,h)
g==h
0 个评论
回答(2 个)
DGM
2024-1-3
移动:Angelo Yeo
2024-1-8
I don't know if there's a generalized canonical way to compare the similarity of graphics objects, but they're going to differ.
% fake data
x = randn(1000,1);
% two objects of the same type, using the same data
h = histogram(x); hold on
g = histogram(x);
% they're not equal
isequal(h,g)
% find where props differ
% this includes properties which aren't public
% though i'm only checking one layer deep
hprops = struct2cell(struct(h));
gprops = struct2cell(struct(g));
equalprops = false(size(hprops));
for k = 1:numel(hprops)
equalprops(k) = isequal(hprops{k},gprops{k});
end
fnames = fieldnames(struct(h));
% a list of the fieldnames and values where they differ
d = [fnames(~equalprops) hprops(~equalprops) gprops(~equalprops)]
Things like handles to their hidden descendant objects will differ. The colors of objects (e.g. line objects in a plot) will differ, and their level in the ui stack will differ.
I'm assuming that a test for equivalence will have to consider which properties actually matter and compare those properties alone.
Dyuman Joshi
2024-1-3
移动:Angelo Yeo
2024-1-8
When you call histogram() again, the handle to the previous histogram is deleted, thus comparing handles results in 0.
The overwriting takes place with most funtions used to plot - plot(), scatter(), surf() etc.
x = randn(1000,1);
f = histogram(x);
g = histogram(x);
whos
f
get(f)
2 个评论
Dyuman Joshi
2024-1-3
移动:Dyuman Joshi
2024-1-8
One such object that does not over-write the previous one is Constant-line objects - https://in.mathworks.com/help/matlab/ref/matlab.graphics.chart.decoration.constantline-properties.html
You can test their equality, and the results are as expected -
y1 = yline(1);
y2 = yline(1);
y1
y2
isequal(y1, y2)
y1==y2
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



