function [han,hax] = plot2ax(x1,y1,x2,y2)
% plot two sets of data with different yaxes
%
%       [han,hax] = plot2ax(x1,y1,x2,y2);
%
% Outputs are the handles to the plot lines (han) and the two axes (hax)
% The displayed x-axis belongs to hax(1)
%
% Quick and dirty example of how to use handle graphics to plot with different
% axes.  The main trick is making the "top" axes transparent so that we
% can see the plot on the "bottom" layer.
%
% y1 and y2 can actually be arrays, which will give multiple lines on the same
% plot.  By saving the final handles to the line objects you can change the colors
% to distinguish between the lines.
%

% John Wilkin 19 April 1999

% plot x1,y1
h1 = plot(x1,y1);

% save the handle to the axes
hax1 = gca;
colororder = get(hax1,'colororder');

% make the axes for the second the same size as the first
hax2 = axes('pos',get(hax1,'pos'));

% plot x2,y2
h2 = plot(x2,y2);
set(h2,'color',colororder(2,:));

% move the yaxis labels to the right and
% make the second axes transparent (so we can see x1,y1) and
% remove the axis lables
set(hax2,'yaxislocation','right','color','none','xticklabel',[]);

% make the xaxis limits are the same so that the data align
set(hax2,'xlim',get(hax1,'xlim'));

if nargout > 0
   % concatenate the plot handles in case you want to alter the line
   % appearance
   han = [h1; h2];
   % return the handles to the axes in case you want to change the x or y
   % limits, ticklabels, or other features of the plot
   hax = [hax1; hax2];
end

