MATLAB: Two y axes in subplot

MATLABmultiple axessubploty axes

Hi all,
I am trying to get a second y axis into an subplot.
figure
handle1 = subplot(3,3,1)
handle2 = axes('YAxisLocation','right','Color','none', ...
'XGrid','off','YGrid','off','Box','off');
This creates an additional axis object on the edge of the whole figure and not on the edge of the subplot. Setting the parent property of axes to handle1 is not possible. Calling axes(handle1) does not help. plotyy() works but has a strange behavior in a certain way. Therefore I want to use a own axis. I have not figured out what plotyy does to solve this problem.
Do you have an idea?
Thanx, Milan

Best Answer

You can use SUBPLOT to set the location for your PLOTYY call.
% Data to plot.
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
% Let's set up our subplot by relying on SUBPLOT
figure
AX = subplot(2,1,1); % Auto-fitted to the figure.
P = get(AX,'pos'); % Get the position.
delete(AX) % Delete the subplot axes
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX,'pos',P) % Recover the position.