title: Matlab: Get coordinates of clicks in figure BUT keep button-callbacks
body: <p>I need a function which gives me the coordinates of a click in my figure window AND the mousebutton which was clicked (left, middle, right or KEY pressed), but I still want to use uicontrol()-buttons. I'm currently using ginput() which works fine but the button callback functions are NOT executed thus I think ginput() overrides them :(
  thanks alot in advance!</p>
  
  <p>edit: the "edit"-code from matt (second pots) does this, but just doesn't offer the functionality WHICH button was clicked:
  <a href="http://www.mathworks.com/matlabcentral/answers/7528-ginput-in-a-gui" rel="nofollow">http://www.mathworks.com/matlabcentral/answers/7528-ginput-in-a-gui</a> :(</p>
  
  <p><strong>EDIT</strong>
  Finally I figured it out myself. I hope you like it. I made changes to the original code from matt:</p>
  
  <pre><code>function varargout = ginput_ax(ha,n)
  if nargin&lt;2
      n=1;
  end
  k = 0;
  button = 0;
  xy = zeros(n,2);
  hf = get(ha,'parent');
  figure(hf);
  set(hf,'WindowButtonMotionFcn',@changepointer)
  set(ha,'ButtonDownFcn',@getpoints)
  hp = get(ha,'children');
  ht = get(hp,'hittest');
  set(hp,'hittest','off')
  axlim = get(ha,'Position');
  fglim = get(hf,'Position');
  x1 = axlim(1)*fglim(3) + fglim(1);
  x2 = (axlim(1)+axlim(3))*fglim(3) + fglim(1);
  y1 = axlim(2)*fglim(4) + fglim(2);
  y2 = (axlim(2)+axlim(4))*fglim(4) + fglim(2);
  waitfor(hf,'WindowButtonMotionFcn',[])
  if iscell(ht)
      for jj=1:length(ht)
          set(hp(jj),'hittest',ht{jj})
      end
  else
      set(hp,'hittest',ht)
  end
  
  % Mouse-Button recognition...
  if(strcmp(button, 'normal'))
      button = 1; % left
  elseif(strcmp(button, 'extend'))
      button = 2; % right
  elseif(strcmp(button, 'alt'))
      button = 3; % middle
  else
      button = 4; % double click any mousebutton
  end
  
  if nargout==3 
      varargout{1} = xy(:,1);
      varargout{2} = xy(:,2);
      varargout{3} = button;
  elseif nargout==2
      varargout{1} = xy(:,1);
      varargout{2} = xy(:,2);
  else
      varargout{1} = xy;
  end
      function changepointer(~,~)
          pntr = get(0,'PointerLocation');
          if pntr(1)&gt;x1 &amp;&amp; pntr(1)&lt;x2 &amp;&amp; pntr(2)&gt;y1 &amp;&amp; pntr(2)&lt;y2
              set(hf,'Pointer','crosshair')
          else
              set(hf,'Pointer','arrow')
          end
      end
      function getpoints(src,evnt)
          cp = get(src,'CurrentPoint');
          button = get(hf, 'SelectionType');
          k = k+1;
          xy(k,:) = cp(1,1:2);
          if k==n
              set(hf,'Pointer','arrow')
              set(hf,'WindowButtonMotionFcn',[])
              set(ha,'ButtonDownFcn',[])
          end
      end
  end
  </code></pre>
  
  <p>Just copy it to a new file like "ginput_ax.m" and than call it via</p>
  
  <pre><code>figure
  ginput_ax(gca, N)
  </code></pre>
  
  <p>whereas <code>N</code>is the number of points captured! Even better than ginput because uicontrol buttons work, double click is recognized and the crosshair-pointer is ONLY displayed within the actual plot, not on the grey border around the plot in the figure window :) Hope you like it!</p>