필터 지우기
필터 지우기

Function findobj does not find an object (?)

조회 수: 11 (최근 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');

카테고리

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