MATLAB: How to correct the problem of an offset in when using plotyy

MATLABplotyyy-axis offset

For some reason, I am having a problem with plotyy, regarding the position of the second y-axis. The data of the second axis is offset from the first axis, and moreover, there is a pesky gap between the plot and the second y-axis tick marks. Can someone please recommend a fix?
Matlab version: 2015b
Relevant portions of code:
[ax,p1,p2] = plotyy(t,u,t,speed,'plot','plot');
xlabel(ax(1),'Time');
ylabel(ax(1),'Control, u');
ylabel(ax(2),'Speed');
axis square; box on;
xlim(ax(1),[0 38]);
xlim(ax(2),[0 38]);
set(ax(2),'position',get(ax(1),'position'));
You will notice that in the last line I attempt to make use of the solution to the similar (or perhaps even same…) problem, which is discussed here: http://www.mathworks.com/matlabcentral/answers/133502-offset-between-2-axes-when-using-plotyy
Given that it has been a while since the previous post, that I am using a newer version of matlab, and since I tried the solution previously recommended, I decided to re-ask this question. Thanks!

Best Answer

Your
axis square; box on;
does not say which of the axes you are applying that to. It is not going to apply it to both of them; it is going to be acting on whichever of the pair is "active". Try
axis(ax(1), 'square')
axis(ax(2), 'square');
box(ax(1), 'on');
but not box(ax(2), 'on') because that would duplicate the box being drawn.