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!



