Matlab:获取图中的点击坐标,但保持按钮回调 [英] Matlab: Get coordinates of clicks in figure BUT keep button-callbacks

查看:786
本文介绍了Matlab:获取图中的点击坐标,但保持按钮回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,它给我的点击在我的图窗口的坐标和鼠标点击(左,中,右或键按下),但我仍然想使用uicontrol() - 按钮。我目前使用ginput()它工作正常,但按钮回调函数不执行,因此我认为ginput()覆盖他们:(
提前感谢alot!



编辑:从马特(第二壶)的编辑代码做到这一点,但只是不提供功能WHICH按钮被点击:
http://www.mathworks.com/matlabcentral/answers/7528-ginput-in-a-gui : (



EDIT
最后我想出了自己,我希望你喜欢它。 :

  function varargout = ginput_ax(ha,n)
if nargin <2
n = 1;
end
k = 0;
button = 0;
xy = zeros(n,2);
hf = get(ha,'parent');
图形(hf);
set(hf,'WindowButtonMotionFcn',@ changepointer)
set(ha,'ButtonDownFcn',@ getpoints)
hp = get(ha,'children' ;
ht = get(hp,'hittest');
set(hp,'hittest','off')
axlim = get(ha,'Position');
fglim = get(hf,'Position');
x1 = axlim(1)* fglim(3)+ fglim(1);
x2 =(axlim(1)+ axlim(3))* fglim(3)+ fglim(1)
y1 = axlim(2)* fglim(4)+ fglim(2);
y2 =(axlim(2)+ axlim(4))* fglim(4)+ fglim(2)
waitfor(hf,'WindowButtonMotionFcn',[])
if iscell(ht)
for jj = 1:length(ht)
set(hp(jj),'hittest ',ht {jj})
end
else
set(hp,'hittest',ht)
end

%鼠标按钮识别。 ..
if(strcmp(button,'normal'))
button = 1; %left
elseif(strcmp(button,'extend'))
button = 2; %right
elseif(strcmp(button,'alt'))
button = 3; %middle
else
button = 4; %双击任何鼠标按钮
end

如果nargout == 3
varargout {1} = xy(:,1);
varargout {2} = xy(:,2);
varargout {3} = button;
elseif nargout == 2
varargout {1} = xy(:,1);
varargout {2} = xy(:,2);
else
varargout {1} = xy;
end
function changepointer(〜,〜)
pntr = get(0,'PointerLocation');
if pntr(1)> x1&& pntr(1)< x2&& pntr(2)> y1&& pntr(2)< y2
set(hf,'Pointer','crosshair')
else
set(hf,'Pointer','arrow')
end
end
function getpoints(src,evnt)
cp = get(src,'CurrentPoint');
button = get(hf,'SelectionType');
k = k + 1;
xy(k,:) = cp(1,1:2);
if k == n
set(hf,'Pointer','arrow')
set(hf,'WindowButtonMotionFcn',[])
set(ha,'ButtonDownFcn ',[])
end
end
end

将它复制到一个新文件,如ginput_ax.m,然后通过

$ p

 
ginput_ax N)

N 得分!甚至比ginput更好,因为uicontrol按钮工作,双击被识别,十字准线指针只显示在实际情节,而不是图形窗口中的情节的灰色边框:)希望你喜欢它!

$找到一个很好的工作解决方案,这甚至比ginput更好,因为十字线只显示在实际情节(因此在轴内)。请参阅上面的编辑!


I need a function which gives me the coordinates of a click in my figure window AND the mousebutton which was clicked (left, middle, right or KEY pressed), but I still want to use uicontrol()-buttons. I'm currently using ginput() which works fine but the button callback functions are NOT executed thus I think ginput() overrides them :( thanks alot in advance!

edit: the "edit"-code from matt (second pots) does this, but just doesn't offer the functionality WHICH button was clicked: http://www.mathworks.com/matlabcentral/answers/7528-ginput-in-a-gui :(

EDIT Finally I figured it out myself. I hope you like it. I made changes to the original code from matt:

function varargout = ginput_ax(ha,n)
if nargin<2
    n=1;
end
k = 0;
button = 0;
xy = zeros(n,2);
hf = get(ha,'parent');
figure(hf);
set(hf,'WindowButtonMotionFcn',@changepointer)
set(ha,'ButtonDownFcn',@getpoints)
hp = get(ha,'children');
ht = get(hp,'hittest');
set(hp,'hittest','off')
axlim = get(ha,'Position');
fglim = get(hf,'Position');
x1 = axlim(1)*fglim(3) + fglim(1);
x2 = (axlim(1)+axlim(3))*fglim(3) + fglim(1);
y1 = axlim(2)*fglim(4) + fglim(2);
y2 = (axlim(2)+axlim(4))*fglim(4) + fglim(2);
waitfor(hf,'WindowButtonMotionFcn',[])
if iscell(ht)
    for jj=1:length(ht)
        set(hp(jj),'hittest',ht{jj})
    end
else
    set(hp,'hittest',ht)
end

% Mouse-Button recognition...
if(strcmp(button, 'normal'))
    button = 1; % left
elseif(strcmp(button, 'extend'))
    button = 2; % right
elseif(strcmp(button, 'alt'))
    button = 3; % middle
else
    button = 4; % double click any mousebutton
end

if nargout==3 
    varargout{1} = xy(:,1);
    varargout{2} = xy(:,2);
    varargout{3} = button;
elseif nargout==2
    varargout{1} = xy(:,1);
    varargout{2} = xy(:,2);
else
    varargout{1} = xy;
end
    function changepointer(~,~)
        pntr = get(0,'PointerLocation');
        if pntr(1)>x1 && pntr(1)<x2 && pntr(2)>y1 && pntr(2)<y2
            set(hf,'Pointer','crosshair')
        else
            set(hf,'Pointer','arrow')
        end
    end
    function getpoints(src,evnt)
        cp = get(src,'CurrentPoint');
        button = get(hf, 'SelectionType');
        k = k+1;
        xy(k,:) = cp(1,1:2);
        if k==n
            set(hf,'Pointer','arrow')
            set(hf,'WindowButtonMotionFcn',[])
            set(ha,'ButtonDownFcn',[])
        end
    end
end

Just copy it to a new file like "ginput_ax.m" and than call it via

figure
ginput_ax(gca, N)

whereas Nis the number of points captured! Even better than ginput because uicontrol buttons work, double click is recognized and the crosshair-pointer is ONLY displayed within the actual plot, not on the grey border around the plot in the figure window :) Hope you like it!

解决方案

Found a good working solution myself, which is even nicer than ginput since the crosshair is only shown within the actual plot (hence within the axes). See my edit above!

这篇关于Matlab:获取图中的点击坐标,但保持按钮回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆