Creating a mouse down event specific to an axes1 component only

16 views (last 30 days)
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
  1 Comment
Jason
Jason on 12 Oct 2015
This seems to work:
if (curX>min(xLimits)&&curX<max(xLimits)&&curY>min(yLimits)&&curY<max(yLimits))
was wondering if there was a more direct way to say if mouse over axes1?

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 12 Oct 2015
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.
  13 Comments
Geoff Hayes
Geoff Hayes on 14 Oct 2015
Yes you can with
handles = guidata(objectHandles)
which gives you the structure of handles to all other controls within the GUI. Look closer at what you were doing with your code
function ImageClickCallback (objectHandle , eventData, handles)
handles = guidata(objectHandle)
You are passing handles as the third input to the above callback only to overwrite it with the first line of code, so passing in this object is unnecessary.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!