MATLAB Examples

This is an example of how to create a plot with two y axes in MATLAB®.

Read about the yyaxis function in the MATLAB documentation. This function is available in R2016a or newer.

For more examples, go to MATLAB Plot Gallery

% Check version
if verLessThan('matlab','9.0')
    error(['yyaxis is available in R2016a or newer. ', ...
        'For older releases, use plotyy instead.'])

% Create some data for the two curves to be plotted
x  = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

% Create a plot with 2 y axes using the yyaxis function
yyaxis left
plot(x, y1)
ylabel('Low Frequency')

yyaxis right
plot(x, y2)
ylabel('High Frequency')

% Add title and x axis label
xlabel('Time in \mu sec.')
title('Frequency Response')