If needed, interpolate your data to get more or less data points. Here's an example
newX = min(xvalues) : 0.01 : max(xvalues);
newY = interp1(xvalues, yvalues, newX);
There are several methods to animate a plot. They all involve updating the plot within a loop, using drawnow. Note that this command updates all existing graphics so if lots of other plots/GUIs/Apps are opened all of them will be updated each time drawnow is called which will add a lot of processing time. To reduce this, remove unneeded graphics prior to animation. Use drawnow limitrate to speed up the animation by skipping some of the graphics updates. Use pause(n) to slow down the animation by inserting short pauses at the end of each iteration: pause(0.1).
Method 1: use an animation technique
h = animatedline(axh, 'color','b');
Method 2: update line coordinates iteratively
The most efficient approach for this method is to produce the line object with all NaN values and then to update the XData, YData, (and ZData, if needed) within the loop.
h = plot(axh, nan(size(x)),nan(size(y)),'b-');
Another method is to plot independent line segments whose properties can be manipulated independently.
x = linspace(-2*pi,2*pi,300);
xseg = [x(1:end-1),x(2:end)];
yseg = [y(1:end-1),y(2:end)];
segColors = jet(size(xseg,1));
h = plot(xseg(i,:),yseg(i,:),'-','LineWidth',3, 'color',segColors(i,:));
Method 3: Create a movie
This uses getframe to store an image of each frame within a loop and movie to play it back. Otherwise, it's the same as method 2 above. h = plot(axh, nan(size(x)),nan(size(y)),'b-');
F(n) = struct('cdata',[],'colormap',[]);
Now play it back twice. Save F to play the movie again without recreating the data and figure.
Best Answer