MATLAB如何获得鼠标点击坐标

时间:2013-02-04 10:15:43

标签: matlab user-interface axes

我使用GUIDE创建了GUI。我有一个轴(tag = axes1)。轴上显示图像。 当我点击图像(内轴)时,我需要获得坐标。 当图像未添加到轴时,我得到坐标值。但是图像以轴显示,不能获得轴。如何获得坐标?

2 个答案:

答案 0 :(得分:23)

假设您使用句柄imageHandle绘制了图像:

imageHandle = imshow(imageObj);

您应该将ButtonDownFcn分配给图像处理,而不是轴处理:

set(imageHandle,'ButtonDownFcn',@ImageClickCallback);

并从此函数获取鼠标坐标,如下所示:

function ImageClickCallback ( objectHandle , eventData )
   axesHandle  = get(objectHandle,'Parent');
   coordinates = get(axesHandle,'CurrentPoint'); 
   coordinates = coordinates(1,1:2);
   %// then here you can use coordinates as you want ...
end

您可以尝试这个小的演示来检查我的答案:

function demoOnImageClick
clc;clear;
imObj = rand(500,500);
figure;
hAxes = axes();
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);

function ImageClickCallback ( objectHandle , eventData )
axesHandle  = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint'); 
coordinates = coordinates(1,1:2);
message     = sprintf('x: %.1f , y: %.1f',coordinates (1) ,coordinates (2));
helpdlg(message);
end

end

答案 1 :(得分:2)

您可以通过

从图像处理中获取轴控制柄
ah = get( imageHandle, 'Parent' );

然后你可以通过

获得鼠标点击位置
p = get( ah, 'CurrentPoint' );
相关问题