如何获得Matlab图形轴上的鼠标点击位置?
我想点击一个Matlab图形,找出点击位置的x和y位置如何获得Matlab图形轴上的鼠标点击位置?,matlab,user-interface,Matlab,User Interface,我想点击一个Matlab图形,找出点击位置的x和y位置 在图形中,我认为有一种方法可以点击直线上的一个点,得到它的x和y坐标。如果没有绘制图形,如何执行相同操作?创建图形后,请尝试 [x_coord, y_coord]=ginput(1); 因此,只需单击一次图形(这就是参数为1的原因),即可获得函数返回的坐标。以下是最优雅的操作方法: function test % create test figure f = figure(1); % set function
在图形中,我认为有一种方法可以点击直线上的一个点,得到它的x和y坐标。如果没有绘制图形,如何执行相同操作?创建图形后,请尝试
[x_coord, y_coord]=ginput(1);
因此,只需单击一次图形(这就是参数为1的原因),即可获得函数返回的坐标。以下是最优雅的操作方法:
function test
% create test figure
f = figure(1);
% set function to call on mouse click
set(f, 'WindowButtonDownFcn', @clicker);
end
% function called on mouse click in the figure
function clicker(h,~)
get(h, 'selectiontype')
% 'normal' for left moue button
% 'alt' for right mouse button
% 'extend' for middle mouse button
% 'open' on double click
get(h, 'currentpoint')
% Current mouse location, in pixels from the lower left.
% When the units of the figure are 'normalized', the
% coordinates will be [0 0] inb lower left, and [1 1] in
% the upper right.
end
也许这也会起作用:
function [loc] = get_image_point (I)
figure('name','Doubleclick to set location');imshow(I);
[c r] = getpts(1);
loc = int32([c r]);
if size(loc,1)>1
loc = [loc(1,1) loc(1,2)];
end
close all;
end
斯蒂芬注意到:
-getpts()
-是一个“图像处理工具箱”函数。
-ginput()
-等待鼠标单击,在单击之前停止执行,并且仅在调用时工作
而
get(h,'currentpoint')
将在任何时候工作,只要您的程序正在运行。当您说“如果没有绘制图形,我如何执行相同操作?”时,不清楚您在问什么。但是您可能想使用ginput()。例如,请看这里:我想点击一个空图上的某个地方(我认为matlab称之为axis,而不是figure,因为图(1)并没有生成图形实际显示的白色画布)。我想为轴预定义x和y坐标的最大和最小限制,当我单击轴上的空白区域时,我想能够将点的x和y坐标存储在变量中