MATLAB 笔刷和感兴趣区域数据
MATLAB Brush and Region of interest data
在MATLAB 2014b中,使用图形上的画笔工具,我可以创建一个矩形感兴趣区域。在按住鼠标左键的同时更新坐标 X 和 Y。当我松开鼠标时,是否可以检索绘图上最后显示的 X 和 Y 坐标数据?
看看下面的示例,它向您展示了一种使用 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
.
在MATLAB 2014b中,使用图形上的画笔工具,我可以创建一个矩形感兴趣区域。在按住鼠标左键的同时更新坐标 X 和 Y。当我松开鼠标时,是否可以检索绘图上最后显示的 X 和 Y 坐标数据?
看看下面的示例,它向您展示了一种使用 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
.