function [ax,pl,ylab]=plotyy(x1,y1,x2,y2,y2label,options)

% PLOTYY  Plot graphs with Y tick labels on left and right side.

%	  PLOTYY(x1,y1,x2,y2) plots y1 vs. x1 with y-axis labeling

%	  on left, and plots y2 vs. x2 with y-axis labeling on right.

%t

%	  PLOTYY(x1,y1,x2,y2,y2label) places an optional y-label on 

%	  the right side of the plot, since MATLAB doesn't normally 

%	  support right-hand side y-labels.

%

% 	  [ax,pl] = PLOTYY(x1,y1,x2,y2) returns the handles of the two

%	  axes created by plotyy in ax and the handles of the lines 

%	  created by plotyy in pl.

%

%	  [ax,pl,ylab] = PLOTYY(x1,y1,x2,y2,y2label) also returns the 

%	  handle of the text object used for y2label in ylab.

%

%	  PLOTYY(x1,y1,x2,y2,y2label,options) allows for an options

%         vector as follows:

%

%		OPTIONS(1) : 0 for linear x-axis, 1 for log x-axis

%		OPTIONS(2) : 0 for linear y1-axis, 1 for log y1-axis

%		OPTIONS(3) : 0 for linear y2-axis, 1 for log y2-axis

%		OPTIONS(4:5) : x-axis limits, in form [xmin xmax]

%		OPTIONS(6:7) : y1-axis limits, in form [y1min y1max]

%		OPTIONS(8:9) : y2-axis limits, in form [y2min y2max]

%

%		Specify NaN for any option to use default value.

%

% 	  For example,

% 	    

%		plotyy(x1,y1,x2,y2,'',[0 1 0 1.5 3 nan nan 0 10])

%

%	  plots y1 vs. x1, y2 vs. x2, with log y1-axis scaling an x-axis

%	  range of 1.5 to 3, the default y1-axis range, and a y2-axis 

%         range of 0 to 10.



%Known bugs:

% Due to a bug in 4.0a on Sun and SGI (and perhaps others)

% setting the ylabels units to normalized (so they stick with the

% axis as the figure is resized) does not work correctly.  In

% this case, the ylabel is still drawn, but it won't follow the axis

% around on the screen if the figure is resized.



% constants:

NUMOPTS  = 9;  % Maximum number of arguments in OPTIONS vector

OI_XLOG	 = 1;  % Index of Options vector for x-axis scaling

OI_Y1LOG = 2;  % Index of Options vector for y1-axis scaling	

OI_Y2LOG = 3;  % Index of Options vector for y2-axis scaling

OI_XMIN  = 4;  % Index of Options vector for x-axis min limit

OI_XMAX	 = 5;  % Index of Options vector for x-axis max limit

OI_Y1MIN = 6;  % Index of Options vector for y1-axis min limit

OI_Y1MAX = 7;  % Index of Options vector for y1-axis max limit

OI_Y2MIN = 8;  % Index of Options vector for y2-axis min limit

OI_Y2MAX = 9;  % Index of Options vector for y2-axis max limit



% error checking:

if nargin<4,

 error('PLOTYY requires at least 4 input arguments.');

elseif nargin>6,

 error('PLOTYY accepts no more than 6 input arguments.');

elseif nargout>3,

 error('PLOTYY returns a maximum of 3 output arguments.');

end



% check for options vector:

if nargin==6,

 options(length(options)+1:NUMOPTS)=nan*ones(NUMOPTS-length(options),1);

else

 options=nan*ones(NUMOPTS,1);  % use all default values

end



% first calculate overall x-axis range:

x1min=min(x1(~isnan(x1)));

x2min=min(x2(~isnan(x2)));

x1max=max(x1(~isnan(x1)));

x2max=max(x2(~isnan(x2)));

xrange=[min(x1min,x2min) max(x1max,x2max)];

if ~isnan(options(OI_XMIN)),

 xrange=[options(OI_XMIN) xrange(2)];

end

if ~isnan(options(OI_XMAX)),

 xrange=[xrange(1) options(OI_XMAX)];

end



% Set up (x2,y2) plot first:

axes2=gca;

p2=plot(x2,y2);

set(gca,'YDir','rev');  % This line and the next are the guts of plotyy.  They

view(360-200*eps,-90);  % allow the function to display y tick labels on the 

			% right hand side of the plot.



% set up (x1,y1) plot now:

axes1=axes('Position',get(axes2,'Pos'));  % create a new axis in same location

					  % as the old axis

p1=plot(x1,y1);



% set both axes to have same x range:

set(axes1,'XLim',xrange);

set(axes2,'XLim',xrange);



% handle OPTIONS vector:

if options(OI_XLOG)==0,

 set(axes1,'XScale','linear');

 set(axes2,'XScale','linear');

elseif options(OI_XLOG)==1,

 set(axes1,'XScale','log');

 set(axes2,'XScale','log');

end

if options(OI_Y1LOG)==0,

 set(axes1,'YScale','linear');

elseif options(OI_Y1LOG)==1,

 set(axes1,'YScale','log');

end

if options(OI_Y2LOG)==0,

 set(axes2,'YScale','linear');

elseif options(OI_Y2LOG)==1,

 set(axes2,'YScale','log');

end

if ~isnan(options(OI_Y1MIN));

 set(axes1,'YLim',get(axes1,'YLim').*[0 1]+[options(OI_Y1MIN) 0]);

end

if ~isnan(options(OI_Y1MAX));

 set(axes1,'YLim',get(axes1,'YLim').*[1 0]+[0 options(OI_Y1MAX)]);

end

if ~isnan(options(OI_Y2MIN));

 set(axes2,'YLim',get(axes2,'YLim').*[0 1]+[options(OI_Y2MIN) 0]);

end

if ~isnan(options(OI_Y2MAX));

 set(axes2,'YLim',get(axes2,'YLim').*[1 0]+[0 options(OI_Y2MAX)]);

end



% now tidy the plot up a bit:

ticklen=get(axes1,'TickLength');

set(axes2,'TickLength',[ticklen(1) ticklen(1)]);

set(axes2,'TickDir',get(axes1,'TickDir'));

set(axes2,'XTickLabels','');

set(axes1,'Box','off');



% If both y1 and y2 are vectors (not matrices) make 

% the color of y2 be the second color in the ColorOrder.

% (This is to make the plot aesthetic.)

[m1,n1]=size(y1);

[m2,n2]=size(y2);

if (m1==1 | n1==1) & (m2==1 | n2==1),

 ColorOrd=get(axes1,'ColorOrder');

 if size(ColorOrd,1)~=1, % in case ColorOrder only has 1 color in it

  set(p2,'Color',ColorOrd(2,:));

 end

end



% create y2label if required:

if nargin > 4,

 if ~isempty(y2label),

  ylab=text(0,0,y2label);

  save_axes1_units=get(axes1,'Units');	% save current state

  set(ylab,'Units','pixels'); 		% convert everything to 

  set(axes1,'Units','pixels');		% same units

  axes1pos=get(axes1,'Position');

% get "typical" ylabel offset from axes, add it to width of axes1

% to get y2label's x-coordinate:

  typical_ylabel_handle=get(axes1,'YLabel');

  typ_ylabel_pos=get(typical_ylabel_handle,'Position');

  ylaboffset=-1*typ_ylabel_pos(1);

  ylabxcoordinate=ylaboffset+axes1pos(3);

% y2label's y-coordinate is same as "typical" value: 

  ylabycoordinate=typ_ylabel_pos(2);



  set(ylab,'Position',[ylabxcoordinate ylabycoordinate]);

  set(ylab,'HorizontalAlignment','center');

  set(ylab,'Rotation',90);

% Due to a bug in 4.0a on Sun and SGI (and perhaps others)

% setting the ylabels units to normalized (so they stick with the

% axis as the figure is resized) does not work correctly.  In

% this case, draw the ylabel, but set it so it won't stick to axis

% position:

if ~strcmp(version,'4.0a'),

  set(ylab,'Units','normalized');

end



% restore old 'units' settings:

  set(axes1,'Units',save_axes1_units);



% y2 label is usually clipped if using default axes position.

% Therefore shrink axes width by 5%

% to leave room for it:

  axespos=get(axes1,'Position');

  set([axes1,axes2],'Position',axespos-[0 0 .05 0]);

 end % ~isempty(y2label)

end % creation of y2label



% return handles to axes and lines:

if nargout>0,ax=[axes1;axes2];end

if nargout>1,pl=[p1;p2];end

