MATLAB: How to optimize graphics update in animations

graphicsMATLABpanningscrollingsmoothspeed

I have a plot with multiple subplots that I would like to create an auto-scrolling effect with. I first set up the figure:
xData = 0:0.001:60;
yData = sin(2*pi*xData);
xLimit = [0,5];
figure;
nAxes = 4; % Number of Axes
for i = 1:nAxes
Ax(i) = subplot(nAxes,1,i);
line('parent',Ax(i),'xdata',xData,'ydata',yData);
end
Then I iteratively update the 'x-axis' limits to achieve continuous panning of the plot.
step = 0.1;
while xLimit(2)<xData(end)
xLimit=xLimit+step;
for i=1:nAxes
xlim(Ax,xLimit)
end
pause(0.00001);
end
A small delay is added in each iteration using the 'pause' function. However, the graphics are not updating quickly enough to achieve smooth panning, and there is a small shake in the y-axis during the animation. A similar issue is reported here:
How do I achieve smoother panning of the plot?

Best Answer

The animation rendering can be improved by changing the way the plot is set up. The main modification is changing the function 'line' to 'animatedline'.
animatedline(Ax(i),xData,yData);
Additional modifications include manually setting the layout of axes (for n axes), turning off the toolbar and default activity, and drawing the line object in the inner position to prevent shaking of the axis.
% Manual layout for n vertically stacked subplots
Ax(i) = axes;
Ax(i).OuterPosition = [0.0; (i-1)*0.25; 1.0; 0.25];
% Turn off toolbar and default interactivity
Ax(i).Toolbar = [];
disableDefaultInteractivity(Ax(i));
% Draw in inner position
drawnow
Ax(i).PositionConstraint = 'innerposition';
These additional modifications should be included in the 'for' loop that sets up the figure in this example.