MATLAB: Do I get an overlapped plot when I use the PLOTYY function with the SUBPLOT function

MATLABoverlapplotyysubplot

Why do I get an overlapped plot when I use the PLOTYY function with the SUBPLOT function?
When I use the following code the resulting figure shows overlapped y-tick labels. All the subplots are also in the background and only one plot is displayed.
A=[1,2,3,4,5,6,7,8];
x=1:8;
for I=1:4
subplot(2,2,I)
plotyy(x,A,x,A);
end
% this command will not work and creates overlapped plot
subplot(2,2,2)
plotyy(x,A,x,A);

Best Answer

This bug has been fixed for Release 14 (R14). For previous releases, please read below for any possible workarounds:
This unexpected behavior results from the way the SUBPLOT and PLOTYY functions are supposed to work. To explain further, the PLOTYY function's part in this problem is that it creates two axes to have the desired y-tick labels on the left and right. The SUBPLOT function, on the other hand, has the following documented remark on page 2-433 of the MATLAB Function Reference: Volume 3 (P-Z), version 6:
If a subplot specification causes a new axes to overlap an existing axes, then subplot deletes the existing axes. However, if the subplot specification exactly matches the position of an existing axes, then the matching axes is not deleted and it becomes the current axes.
As a result subplot will delete the previous axes when you refer to the existing subplot again. The next command will be treated as if you are plotting using the PLOTYY function on the figure.
This information can also be seen in the online documentation:
In order to avoid this problem, please use the subplot function twice as suggested in the following example. This will eliminate the problem:
A=[1,2,3,4,5,6,7,8];
x=1:8;
% figure number one using plotyy
figure;
for I=1:4
subplot(2,2,I)
plotyy(x,A,x,A);
end
% this command will work now
subplot(2,2,2)
subplot(2,2,2)
plotyy(x,A,x,A);
Our development staff has also been notified of this behavior and is currently looking into addressing this problem in a future release of MATLAB.