Animating plotyy() using refreshdata()

Hi all, I've managed to animate a single plot using refreshdata() as follows:
function test()
x=1:0.1:100;
y=sin(x);
x1=x(1); y1=y(1);
h = plot(x1,y1,'linesmoothing','on');
set(h,'ydatasource','y1'); set(h,'xdatasource','x1');
axis([0,100,-2,2])
hold on
for n=1:1000
x1=x(1:n);
y1=y(1:n);
refreshdata(h,'caller');
drawnow;
pause(0.01);
end
hold off
end
My problem is that for in order for refreshdata() to work I have to use set(h,'ydatasource','y1'...) before the loop. How would I do this when I have two Y data sources as in plotyy()?
Thanks, Dan

回答(1 个)

Daniel - one of the examples from plotyy is as follows
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
figure % new figure
plotyy(x,y1,x,y2)
If you assign the output from plotyy as
h = plotyy(x,y1,x,y2)
you will notice that h is a 1x2 array of axes handles. In order to get the graphics object for each, you would have to do something like
hPlot1 = get(h(1),'Children');
hPlot2 = get(h(2),'Children');
In the above, we assume that each axes has one child only, and that that child is the graphic handles to the line that has been plotted. You would then repeat what you have already done for these two graphics object handles
set(hPlot1,'ydatasource','y1'); set(h,'xdatasource','x1');
set(hPlot2,'ydatasource','y2'); set(h,'xdatasource','x2');
The above is just an example - I don't know if you have a x2 that is different from x1. Then, in your for loop, you would refresh both as
refreshdata(hPlot1,'caller');
refreshdata(hPlot2,'caller');
Try the above and see what happens!

2 个评论

Thank you so much, worked like a charm first time round :D
Glad it worked out, Daniel!

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by