MATLAB: Only plotting on the last of three axes

axesmultiple axes

I have three axes on the same figure. Each axes has its own plot function. The first time the code runs it prints on the correct axes. On the second time the functions are called, all three plots are shown in the last axes in sequence Tried cla, cla reset, and many others Does this have an obvious solution which I am missing?

Best Answer

Each axes has its own plot function. What does that mean?
If a figure has several axes, that means you used subplot to create the axis. You need to save the handle of these axis in order to replot in them:
figure;
hax(1) = subplot(1, 3, 1); %save handle of 1st axis
plot(sin(linspace(0, 2*pi, 200)));
hax(2) = subplot(1, 3, 2); %save handle of 2nd axis
plot(sqrt(linspace(0, 5, 200)));
hax(3) = subplot(1, 3, 3); %save handle of 3rd axis
plot(log(linspace(0, 5 , 200)));
To replot in all axis
plot(hax(1), tan(linspace(0, 2*pi, 200))); %plot in first axis
plot(hax(2), linspace(0, 5, 200).^2); %plot in second axis
plot(hax(3), exp(linspace(0, 5, 200))); %plot in 3rd axis
Alternatively, if you don't want to store the axis, you can reissue the subplot command:
subplot(1, 1, 1); %replot in first axis
plot(cos(linspace(0, 2*pi, 200)));
subplot(1, 1, 2); %replot in second axis
plot(linspace(0, 5, 200));
subplot(1, 1, 2); %replot in 3rd axis