Duplicate subplots within loop

2 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 30 Nov 2018
"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 Comment
Jens Gaechter
Jens Gaechter on 30 Nov 2018
Thanks Walter for the fast reply and your explanation!

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!