答案 0 :(得分:2)
看一下下面的示例,该示例向您展示了使用listener鼠标按钮释放操作的方法。
更新:根据评论,OP需要起点和终点。
为正在按下的鼠标添加了一个监听器,该监听器将这些点存储在轴用户数据中,然后在释放鼠标按钮时与坐标一起显示。
function display_coordinates_example
% create a figure
f = figure;
% create an axes
ax = axes;
% plot your data
plot ( 1, 1, 'o');
% add a listener to be activated when the mouse button is released.
addlistener ( f, 'WindowMousePress', @(a,b)mousepress(ax) );
addlistener ( f, 'WindowMouseRelease', @(a,b)mouserelease(ax) );
end
function mousepress ( ax )
ax.UserData.cp = ax.CurrentPoint(1,1:2);
end
function mouserelease ( ax )
% display the current X,Y position.
startCp = ax.UserData.cp;
cp = ax.CurrentPoint(1,1:2);
fprintf ( 'X = [%f, %f] and Y = [%f, %f]\n', startCp(1,1), cp(1,1), startCp(1,2), cp(1,2) )
end
更新2
如果您没有使用brush
,那么您可以使用该标准的标准Matlab回调,例如。
set ( f, 'WindowButtonDownFcn', @(a,b)mousepress(ax) );
set ( f, 'WindowButtonUpFcn', @(a,b)mouserelease(ax) );
尝试一下 - 在刷数据之前,你会发现它有效。但是当你刷数据时,Matlab使用这些回调进行刷牙 - >所以他们暂时被禁用...要解决这个问题,你必须使用鼠标按下并释放听众。
systax @(a,b)mousepress(ax)
可以替换为更标准的回调:
addlistener ( f, 'WindowMousePress', @mousepress );
在回调中使用此语法,您必须找到轴句柄,因为默认情况下输入将是图形句柄f和鼠标事件信息 -
function mousepress ( f, event )
ax = findobj ( f, 'type', 'axes' );
ax.UserData.cp = ax.CurrentPoint(1,1:2);
end
在我看来, findobj
浪费时间 - 你创建它时就有了句柄,所以我们保留它,而不是在我们需要它的时候找到它。
我所做的是将感兴趣的项目(ax
)传递给回调而不传递我不感兴趣的其他项目。我这样做是通过创建anonymous函数。 a,b
代表正常的回调输入fig and event data
,但我没有将它们传递给回调函数 - 而是传入感兴趣的变量 - ax
。