MATLAB: How to add the third time series to the chart which is to be read on the right hand axis with the first two time series plotted on the same chart on the left hand axis in MATLAB 7.6 (R2008a)

axishandholdMATLABplotyyrightsqueezetimeseries

I have three time series. I would like to chart two of them on the same axis (say the left hand axis), which is straightforward. but I would also like to add the third series to the chart, to be read on the right hand axis.

Best Answer

You may plot multiple lines on both axes by either concatenating the data or using the HOLD function. For example, let’s say that you want to plot 2 time-series plots for the first y-axis and 1 time-series plot for the second y-axis.
% 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);
% plotting the data on the two axes
[AX H1 H2] = plotyy(x, [y1; y2], x, [y3]);
Note that each of the time-series vectors y1 through y3 are row vectors. Their vertical concatenation allows us to plot all of them simultaneously.
Alternatively, you can do this using the HOLD function:
[AX H1 H2] = plotyy(x, y1, x, y3);
hold(AX(1), 'on')
plot(AX(1),x,y2,'c');
If your time series are stored in a “timeseries” object, you will need to extract the data from them to plot. For example,
t1 = timeseries(rand(1, 10), 1:10);
x = t1.Time
y = squeeze(t1.Data)