MATLAB: How to add additional plots to either of the two y-axes when using the PLOTYY function

MATLABmultipleplotyy

I am using the PLOTYY function to create a single plot with one x-axis and two y-axes. I am plotting a time-series vector on each of these y-axes using the following statement:
plotyy(x, y1, x, y2, 'plot');
I would like to know how to add additional plots to either of these two y-axes. For example, I may want 3 time-series plots for the first y-axis and 2 time-series plots for the second y-axis.

Best Answer

You may plot multiple lines on both axes by either concatenating the data or using the HOLD function. For example:
% define the data
figure
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 1./exp(-0.05*x).*cos(x);
y3 = 200*cos(x);
y4 = 0.8*exp(-0.5*x).*sin(10*x);
y5 = 0.8*exp(-0.5*x).*cos(x);
% plotting the data on the two axes
[AX H1 H2] = plotyy(x, [y1; y2; y3], x, [y4; y5], 'plot');
Note that each of the time-series vectors y1 through y5 are row vectors. Their vertical concatenation allows us to plot all of them simultaneously.
Using the HOLD function:
figure
%plot data on both axes
[AX H1 H2] = plotyy(x, y1, x, y4, 'plot');
%hold both axes
hold(AX(1));
hold(AX(2));
%plot additional data to the axes
plot(AX(1),x,y2,'c');
plot(AX(1),x,y3,'r');
plot(AX(2),x,y5,'m');