# Matlab: replace one plot maintaining others

I have a figure in which I plot some disperse points and then a trajectory. I want to switch between different trajectories by plotting them in the same figure as the points, but without creating new figures, i.e., "erasing" the first trajectory and then plotting the new one.

Is there a way of doing this?

## Answers

Perhaps this little demo will be helpful:

xy = rand(20,2); figure % Plot first iteration and output handles to each h = plot(xy(:,1),xy(:,2),'b.',xy(1:2,1),xy(1:2,2),'r-'); axis([0 1 0 1]) % Update second plot by setting the XData and YData properties of the handle for i = 2:size(xy,1)-1 set(h(2),{'XData','YData'},{xy(i:i+1,1),xy(i:i+1,2)}) drawnow pause(0.1); end

You should read up on handle graphics in Matlab and the get and set functions.