%script myzoom: zoom in on or out from box given by two clicks of the mouse. 
% 2 LEFT clicks: zoom in to given zone
% 2 MIDDLE clicks: zoom out to bring current graph onto clicked zone
% 2 RIGHT clicks: stop zooming. 

YES =1; 
while(YES ==1)
 [X,Y,Button] = ginput(2); 
 if Button(2) == 1, 
  set(gca,'Xlim', [min(X) max(X)]); 
  set(gca,'Ylim', [min(Y) max(Y)]); 
 end
 if Button(2) == 2, 
  X2 = get(gca,'Xlim');
  Y2= get(gca,'Ylim'); 
  X1 = [min(X) max(X)];
  Y1 = [min(Y) max(Y)]; 
  ratioX = (X2(2)-X2(1))/(X1(2)-X1(1)); 
  ratioY = (Y2(2)-Y2(1))/(Y1(2)-Y1(1)); 
  X3 = (X2-X2(1))*ratioX + X2(1)-(X1(1)-X2(1))*ratioX;
  Y3 = (Y2-Y2(1))*ratioY + Y2(1)-(Y1(1)-Y2(1))*ratioY;
  set(gca,'Xlim',X3);   
  set(gca,'Ylim',Y3); 
 end
 if Button(2) == 3, 
  YES = 0; 
 end
end

clear Button Yes X Y
