Azzera filtri
Azzera filtri

Function findobj does not find an object (?)

11 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Cris LaPierre
Cris LaPierre il 10 Gen 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

Più risposte (1)

Matt J
Matt J il 10 Gen 2022
h = findobj(gcf,'Type', 'Scatter');

Categorie

Scopri di più su Discrete Data Plots in Help Center e File Exchange

Tag

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by