Function findobj does not find an object (?)

11 次查看(过去 30 天)
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?

采纳的回答

Cris LaPierre
Cris LaPierre 2022-1-10
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

更多回答(1 个)

Matt J
Matt J 2022-1-10
h = findobj(gcf,'Type', 'Scatter');

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by