Checking if two handle objects are identical using 'isequal' and '=='

9 Ansichten (letzte 30 Tage)
Jiyeol Oh
Jiyeol Oh am 3 Jan. 2024
Verschoben: Dyuman Joshi am 8 Jan. 2024
I am studying object and class in MATLAB and I found something I can't understand.
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)
ans = logical
0
g==h
ans = logical
0

Antworten (2)

DGM
DGM am 3 Jan. 2024
Verschoben: Angelo Yeo am 8 Jan. 2024
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)
ans = logical
0
% 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)]
d = 12×3 cell array
{'AutoColor' } {[ 0 0.4470 0.7410]} {[ 0.8500 0.3250 0.0980]} {'Face' } {1×1 Quadrilateral } {1×1 Quadrilateral } {'Edge' } {1×1 LineStrip } {1×1 LineStrip } {'BrushFace' } {1×1 Quadrilateral } {1×1 Quadrilateral } {'BrushEdge' } {1×1 LineStrip } {1×1 LineStrip } {'NodeChildren' } {4×1 Graphics } {4×1 Graphics } {'DataTipTemplate' } {1×1 matlab.graphics.datatip.DataTipTemplate} {1×1 matlab.graphics.datatip.DataTipTemplate} {'Behavior' } {1×1 struct } {1×1 struct } {'Behavior_I' } {1×1 struct } {1×1 struct } {'SerializableBehavior'} {1×1 struct } {1×1 struct } {'SeriesIndex' } {[ 1]} {[ 2]} {'SeriesIndex_I' } {[ 1]} {[ 2]}
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.
  1 Kommentar
Jiyeol Oh
Jiyeol Oh am 3 Jan. 2024
Verschoben: Angelo Yeo am 8 Jan. 2024
Very interesting. Thank you for your great answer, DGM!
Things like levels in the UI stack are something I've never been considered before..
Testing the equivalence of two handle objects with the predetermined properties looks clear

Melden Sie sich an, um zu kommentieren.


Dyuman Joshi
Dyuman Joshi am 3 Jan. 2024
Verschoben: Angelo Yeo am 8 Jan. 2024
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
Name Size Bytes Class Attributes cmdout 1x33 66 char f 1x1 8 matlab.graphics.chart.primitive.Histogram g 1x1 8 matlab.graphics.chart.primitive.Histogram x 1000x1 8000 double
f
f =
handle to deleted Histogram
get(f)
Error using matlab.graphics.chart.primitive.Histogram/get
Invalid or deleted object.

Error in hggetdisp (line 8)
v = get(h,sp);
  2 Kommentare
Jiyeol Oh
Jiyeol Oh am 3 Jan. 2024
Verschoben: Angelo Yeo am 8 Jan. 2024
Thank you!
Great answer to stupid question... lol
Dyuman Joshi
Dyuman Joshi am 3 Jan. 2024
Verschoben: Dyuman Joshi am 8 Jan. 2024
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
y1 =
ConstantLine with properties: InterceptAxis: 'y' Value: 1 Color: [0.1500 0.1500 0.1500] LineStyle: '-' LineWidth: 0.5000 Label: '' DisplayName: '' Use GET to show all properties
y2
y2 =
ConstantLine with properties: InterceptAxis: 'y' Value: 1 Color: [0.1500 0.1500 0.1500] LineStyle: '-' LineWidth: 0.5000 Label: '' DisplayName: '' Use GET to show all properties
isequal(y1, y2)
ans = logical
1
y1==y2
ans = logical
0

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by