title: MATLAB: How to store clicked coordinates using ButtonDownFcn
body: <p><strong>Goal:</strong> To perform several clicks in one figure, containing an image displayed with imshow and save the coordinates of the "clicked" point(s), to be used in further operations. </p>
  
  <p><strong>Notes:</strong> I know about the functions <code>getpts</code>/<code>ginput</code> but I would like to perform this without using them. Is this possible using <code>ButtonDownFcn</code>? (see the following code)</p>
  
  <pre><code>function testClicks
  img = ones(300); % image to display
  h   = imshow(img,'Parent',gca);
  set(h,'ButtonDownFcn',{@ax_bdfcn});
  
  function ax_bdfcn(varargin)
  a = get(gca,'CurrentPoint');
  x = a(1,1);
  y = a(1,2);
  </code></pre>
  
  <p>At this stage the variables <code>x</code> and <code>y</code> only "live" inside <code>ax_bdfcn</code>.
  How can I make them available in the <code>testClicks</code> function? Is this possible using <code>ButtonDownFcn</code>? Is this a good approach?</p>
  
  <p>Thanks a lot.</p>
  
  <p><em><strong>EDIT1:</em></strong>
  Thanks for the answer Shai. But I still cannot accomplish what I intended.</p>
  
  <pre><code>function [xArray, yArray] = testClicks()
  img = ones(300); % image to display
  h   = imshow(img,'Parent',gca);
  x = [];
  y = [];
  xArray = [];
  yArray = [];
  stop = 0;
  while stop == 0;
      set(h,'ButtonDownFcn',{@ax_bdfcn});
      xArray = [xArray x];
      yArray = [yArray y];
          if length(xArray)&gt;15
              stop = 1;
          end
  end
  
      function ax_bdfcn(varargin)
          a = get(gca, 'CurrentPoint');
          assignin('caller', 'x',  a(1,1) );
          assignin('caller', 'y',  a(1,2) );
      end
  end % must have end for nested functions
  </code></pre>
  
  <p>This code (buggy!) is the closest I can get to what I want (after all the clicking, having an array with the x and y coordinates of the clicked points). I am clealy not understanding the mechanics for the implementation of this task. Any help?</p>