Function findobj does not find an object (?)

8 ビュー (過去 30 日間)
Daigo
Daigo 2022 年 1 月 10 日
コメント済み: Daigo 2022 年 1 月 10 日
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');
  1 件のコメント
Daigo
Daigo 2022 年 1 月 10 日
Thank you, Matt!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by