tim
tim

Reputation: 10176

Matlab: Get coordinates of clicks in figure BUT keep button-callbacks

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!

Upvotes: 4

Views: 5406

Answers (1)

tim
tim

Reputation: 10176

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!

Upvotes: 1

Related Questions