MATLAB: Creating a mouse down event specific to an axes1 component only

axes1imagemouseclick

I am using the following to allow the user to use the mouse press to draw a marker on an image loaded to an axes component (axes1). There are other axes also on my GUI, and I want to be able to constrain this mouse action only if the mouse is over axes 1. Currently it also draws a marker when over other axes.
I put this in the opening function.
set(gcf, 'WindowButtonDownFcn', @getMousePositionOnImage);
And then use this function for the mouse click.
function getMousePositionOnImage(src, event)
handles = guidata(src);
cursorPoint = get(handles.axes1, 'CurrentPoint');
curX = cursorPoint(1,1);
curY = cursorPoint(1,2);
%check if mouse clicked outside of axes component
xLimits = get(handles.axes1, 'xlim');
yLimits = get(handles.axes1, 'ylim');
if (curX<min(xLimits)) (curX==min(xLimits))
end
if (curX>max(xLimits)) (curX==max(xLimits))
end
if (curY<min(yLimits)) (curY==min(yLimits))
end
if (curY>max(yLimits)) (curY==max(yLimits))
end
x=round(curX)
y=round(curY)
setappdata(0,'curX',x);
setappdata(0,'curY',y);
%Remove previous marker if present
delete( findobj(gca, 'type', 'line') );
hold on
plot(x,y,'ro','MarkerSize',10,'LineWidth',2)
hold off;
IMG = getimage(handles.axes1);
delta=50;
if (x<delta+1)|| (y<delta+1)
ROI = IMG(1:delta, 1:delta,:); % This is the ROI (indexing is (ymin:ymax, xmin:xmax)

else
ROI = IMG(y-delta:y+delta, x-delta:x+delta,:); % This is the ROI (indexing is (ymin:ymax, xmin:xmax)
end
%Now take a ROI around the marker position (i.e. where mouse is clicked)
%and draw in axes 3
axes(handles.axes3)
cla
[high,low]=Autoscaleimage(handles,ROI,3);
imshow(ROI,[low,high]);
hold on
plot(delta+1,delta+1,'ro','MarkerSize',10,'LineWidth',2);
hold off

Best Answer

Jason - in the OpeningFcn of your GUI, assign the getMousePositionOnImage to the axes and not the figure. Try the following
set(handles.axes1,'ButtonDownFcn',@getMousePositionOnImage);
When I try the above on OS X 10.9.5 with MATLAB R2014a, the getMousePositionOnImage only fires when I press the mouse button down within the axes.
Related Question