Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the mouse click position on Matlab figure axes?

I want to click in a Matlab figure and find out the x and y position of the clicked position.

In a graph I think there is a way to click a point on the line and get it's x and y coordinates. How do I do the same if there is no graph plotted?

like image 537
user13267 Avatar asked Dec 08 '22 19:12

user13267


1 Answers

Here's how to do it most elegantly:

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
like image 61
Rody Oldenhuis Avatar answered Mar 17 '23 03:03

Rody Oldenhuis