フィルターのクリア

When copying plot from one subplot position to another using copyobj, the second plot exhibits "hold on" behavior while the first doesn't.

10 ビュー (過去 30 日間)
Here's a bare bones version of the code that illustrates the problem. I want both plots to not hold on to previous iterations. While the plot on the top does not hold on to previous plots, the zoomed in plot does. I have even tried putting "hold off;" after every line following subplot(2,1,2) and it does not help.
clear; close all; clc;
counter = 0;
r = 0;
position = [0,0];
while counter < 100
subplot(2,1,1)
fimplicit(@(x,y) (x-position(1)).^2+(y-position(2)).^2-r.^2,'r')
r = r + 0.1; position = position + [0.1,0];
ax = gca; axis equal; axis([-10 10 -10 10]);
zoomed = subplot(2,1,2);
copyobj(get(ax,'Children'),zoomed)
axis equal; axis([-1 1 -1 1]);
pause(0.1)
counter = counter + 1;
end

採用された回答

Walter Roberson
Walter Roberson 2023 年 2 月 23 日
None of the low-level graphics operations pay attention to the hold state; they just add to (or modify) what is there already. The hold state is only paid attention to by the high-level plotting operations such as plot() and surf(), which contain code that specifically detects the hold state and clears the axes if the hold is not active.
When you copyobj() you are not invoking any high-level graphics operations, so the object is just added to whatever is there already.
You should cla() as needed.
You should also re-write the code so that every graphics operation is specific about the object it is going to act on.
ax = subplot(2,1,1)
fimplicit(ax, @(x,y) (x-position(1)).^2+(y-position(2)).^2-r.^2,'r')
r = r + 0.1; position = position + [0.1,0];
axis(ax, 'equal'); axis(ax, [-10 10 -10 10]);
zoomed = subplot(2,1,2);
cla(zoomed);
copyobj(get(ax,'Children'),zoomed)
axis(zoomed, 'equal'); axis(zoomed, [-1 1 -1 1]);
pause(0.1)
counter = counter + 1;
  1 件のコメント
Jacob Ward
Jacob Ward 2023 年 2 月 23 日
Thanks! Makes sense that all that was needed was to clear the zoomed axes of what was there before.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 2 月 23 日
You never called hold on for the top plot so why should it hold onto previously plotted stuff?
  1 件のコメント
Jacob Ward
Jacob Ward 2023 年 2 月 23 日
Exactly, I do not want either one to hold on. I never called hold on anywhere, but yet the second plot is holding on.

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

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by