【问题标题】:Get coordinates of clicks in figure AND specific mouse button clicked for each point clicked获取图中的点击坐标和为每个点击点点击的特定鼠标按钮
【发布时间】:2014-05-29 12:28:22
【问题描述】:

Matlab: Get coordinates of clicks in figure BUT keep button-callbacks 的后续问题。

很棒的代码。

提供的函数只输出最后一次点击的“按钮”。有没有办法修改代码以输出为每个选定点单击的特定鼠标按钮?

另外,有没有办法让第三个输入可以用来指定有未知数量的点要选择,并继续选择点,直到像'c'或'return'是用键盘输入的吗?

【问题讨论】:

    标签: matlab button user-interface click mouse


    【解决方案1】:

    关于您的第二个要求,当用户按下一个键时,控件从图形窗口移动到命令窗口,除非您使用 waitforbuttonpress ,这会使事情变得不必要地复杂化。因此,可以建议替代解决方案使用双击来表示输入坐标的结束。这是在下面显示的链接stackoverflow code 的修改版本中完成的。

    代码

    function varargout = ginput_ax_mod2(ha,n)
    if nargin<2
        n=1;
    end
    k = 0;
    button = 0;
    
    %%// Tolerance so that in the inifnity case, this could act as
    %%// the thresholding distance below which the
    %%// input extracting operation must be terminated
    TOL = 0.01;
    
    %%// Placeholders for X-Y and button type could be stored 
    button1 = [];
    xy = [];
    
    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
    selType = get(hf,'SelectionType');
    
    % 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} = button1(:,1);
    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;
    
            if k==1
                xy = [xy ;cp(1,1:2)];
                button1 = [button1; {button}];
            end
    
            if k>=2
                if pdist2(cp(1,1:2),xy(k-1,:))<TOL && isinf(n)
                    k = n;
                else
                    xy = [xy ;cp(1,1:2)];
                    button1 = [button1; {button}];
                end
            end
            if k==n
                set(hf,'Pointer','arrow')
                set(hf,'WindowButtonMotionFcn',[])
                set(ha,'ButtonDownFcn',[])
                return;
            end
        end
    end
    

    样本运行

    运行 1:无限点案例

    [x_coords,y_coords,button_types] = ginput_ax_mod2(gca, Inf)

    运行2:10分案例

    [x_coords,y_coords,button_types] = ginput_ax_mod2(gca, 10)

    无限大小写但在第三次单击后双击的示例输出 -

    x_coords =  
        2.0472
        4.3076
        5.9873
    
    y_coords =
       24.4152
       25.2924
       26.7544
    
    button_types = 
        'normal'
        'alt'
        'normal'
    

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多