Function findobj does not find an object (?)

7 vues (au cours des 30 derniers jours)
Daigo
Daigo le 10 Jan 2022
Commenté : Daigo le 10 Jan 2022
I have a function to make a plot of two vectors t and y, and mark the minimum value of y as a red asterisk marker.
function m = plot_cos(y, t)
figure;
plot(t, y, 'b--'); hold on;
[m,I] = min(y);
s = scatter(t(I),m);
s.MarkerEdgeColor = 'r';
s.Marker = '*';
end
I should be able to use a 'findobj' function to see that the Marker property is set to be the asterisk, that is,
t = linspace(0,2*pi,100);
y = cos(t);
m = plot_cos(y, t)
h = findobj(gcf,'Type', 'Line');
assert(strcmp(h.Marker, '*'));
However, the assertion in the last line of the code fails. Is there any way to set the marker type to asterisk such that 'findobj' can find it?

Réponse acceptée

Cris LaPierre
Cris LaPierre le 10 Jan 2022
assert is for comparison. Your code is checking if the marker of the found line is an asterisk. It is not, so the result is the assertion fails.
Your asterisk was created by scatter, so is a scatter object. To find it, your type should be 'scatter'.
t = linspace(0,2*pi,100);
y = cos(t);
m = plot_cos(y, t)
m = -0.9995
h = findobj(gcf,'Type', 'Scatter');
assert(strcmp(h.Marker, '*'));
function m = plot_cos(y, t)
figure;
plot(t, y, 'b--'); hold on;
[m,I] = min(y);
s = scatter(t(I),m);
s.MarkerEdgeColor = 'r';
s.Marker = '*';
end

Plus de réponses (1)

Matt J
Matt J le 10 Jan 2022
h = findobj(gcf,'Type', 'Scatter');

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by