If you are still interested in the problem, then consider the following example.
Basically we maintain a buffer of values, used to set the lines data each iteration. We turn off automatic axis limits, and instead update them ourselves only when necessary.
The resulting animation is fast and responsive (I actually slowed it down with a small PAUSE), especially since we only maintain values for the visible portion of the lines (we simply discard/overwrite old values).
I am using two 1D random walk signals instead of cosine functions. These sequences are expected to keep growing in both directions, with the axis continuously adjusting its limits. The code can be easily changed to plot more than two signals.
%# setup axis and lines
N = 60; %# window size (60 sec)
XLIMS = [1 N]; %# starting axis limits
YLIMS = [-1 1];
hAx = axes('XLim',XLIMS, 'YLim',YLIMS, 'Box','on', ...
'YLimMode','manual', 'XLimMode','manual');
hLine1 = line('XData',1:N, 'YData',nan, 'Color','b', ...
'Parent',hAx, 'YLimInclude','off');
hLine2 = line('XData',1:N, 'YData',nan, 'Color','r', ...
'Parent',hAx, 'YLimInclude','off');
%# initialize vectors
y1 = nan(N,1);
y2 = nan(N,1);
ind = 1;
val1 = 0; val2 = 0;
while true
%# get new values, and insert them in vectors
val1 = val1 + (rand-0.5);
val2 = val2 + (rand-0.5);
y1(ind) = val1;
y2(ind) = val2;
%# update lines data
set(hLine1, 'YData',y1)
set(hLine2, 'YData',y2)
%# keep track of smallest/largest values seen
mn = min(val1,val2); mx = max(val1,val2);
if mn<YLIMS(1), YLIMS(1) = mn; flag = true; end
if mx>YLIMS(2), YLIMS(2) = mx; flag = true; end
%# update axis Y-limits if needed
if flag
set(hAx, 'YLim',YLIMS); flag = false;
end
%# refresh plot
drawnow, pause(0.02)
%# circularly increment counter
ind = ind + 1;
if ind>N
%# perparing for next cycle
ind = 1;
y1(:) = nan; y2(:) = nan;
%# update axis x-limits and slide line x-data
set(hAx, 'XLim',get(hAx,'XLim')+N);
set(hLine1, 'XData',get(hLine1,'XData')+N);
set(hLine2, 'XData',get(hLine2,'XData')+N);
end
%# break in case you close the figure
if ~ishandle(hAx), break, end
end
