function makeClickableFig(w)
% Make a figure that can respond to mouse clicks.
% Axes limits are [-w/2 w/2 -w/2 w/2].
%
% Every time a click is made inside this figure the global variable click
% is updated to [x y] representing the clicked position in the window.
% Other functions can make use of this global variable.
%
% Be sure to set variable click to empty once you've used it, otherwise
% you can't distinguish the case where the same point is clicked more than
% once.

close all 
h = figure;                     % Create a new figure and save its "handle"
axis([-w/2 w/2 -w/2 w/2], 'square')
set(h, 'WindowButtonDownFcn', @updateClick)
% The 'WindowButtonDownFcn' property of the figure is now set to be
% a handle to the subfunction updateClick.  This means that when a click is
% made in the figure the subfunction updateClick will be called automatically.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function updateClick(source, eventData)
% Update the global variable click to store the most recent mouse click.
% This function is called automatically when someone clicks on the figure.

% Declare a global variable, click.  This means that variable click is 
% accessible in another function that has the same global declaration.
global click
% At this first global declaration, click is initialized as []. 

% Determine where the mouse click is with respect to the current axis
pt = get(gca, 'CurrentPoint');  % array of property values for current click 
click = pt(1, 1:2);             % vector of length 2: x and y coordinates
% disp(['Click  ' num2str(click)])  % Uncomment this to show values for debugging
