Duplicate subplots within loop

조회 수: 2 (최근 30일)
Jens Gaechter
Jens Gaechter 2018년 11월 30일
댓글: Jens Gaechter 2018년 11월 30일
Hi everyone,
i would like to duplicate plots in 2 subplots with different axis settings within a loop. What i want to do is to duplicate the plot from the first subplot and then change the axis properties. If i start with something like this:
x = linspace(-2*pi,2*pi,100);
figure
for i=1:100
y = sin(x+i);
subplot(2,1,1)
hCurve = plot(x,y,'r');
drawnow
subplot(2,1,2)
hSub2 = subplot(2,1,2);
copyobj(hCurve,hSub2);
drawnow
end
the top subplot gets refreshed every iteration, while for the bottom subplot the hold property seems to be on. Changing this property via axis handle doesn't seem to work. I suspect the copyobj function is the key, but how do i achieve the same behavior on both plots?
Thank you very much!

채택된 답변

Walter Roberson
Walter Roberson 2018년 11월 30일
"while for the bottom subplot the hold property seems to be on"
copyobj() adds objects to the existing destination. You generate a line in the first axes, copy it to the second axes. The next iteration, because hold is not on in the first axes, the plot() call removes the children of the first axes and adds a new line. Then you copy that new line to the second axes, but you have not remove the first line.
This is not a bug with the hold property: the hold property does not apply to situations where you copyobj(). The hold property only applies to "high level graphic routines" such as plot() but not line(), or imshow(), or image() that passes in data positionally but not image() that passes parameters as name/value pairs, or surf() (high level) but not surface() (low level).
The behaviour of "hold on" is more or less a courtesy to make using higher level graphics routines more convenient, and does not apply when low level routines such as copyobj() are used.
In other words, if you want the old content removed from subplot 212 then you will need to remove it.
  댓글 수: 1
Jens Gaechter
Jens Gaechter 2018년 11월 30일
Thanks Walter for the fast reply and your explanation!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by