Checking if two handle objects are identical using 'isequal' and '=='
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
DGM
el 3 de En. de 2024
Movida: Angelo Yeo
el 8 de En. de 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)
% 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.
1 comentario
Dyuman Joshi
el 3 de En. de 2024
Movida: Angelo Yeo
el 8 de En. de 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
f
get(f)
2 comentarios
Dyuman Joshi
el 3 de En. de 2024
Movida: Dyuman Joshi
el 8 de En. de 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
y2
isequal(y1, y2)
y1==y2
Ver también
Categorías
Más información sobre Graphics Object Programming en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!