How can I continuously get the mouse coordinates?

3 views (last 30 days)
I'm trying to track the position of the mouse as it scrolls across the figure, so I need the coordinates relative to that figure's axes. The best I have so far is this:
% code
function [x, y] = mouseMove(hobject,eventdata)
C = get(gca,'currentpoint');
x = C(1);
y = C(2);
end
axis equal;
axis([-8,8,-7,18]);
hold on;
set(gcf,'WindowButtonMotionFcn',@mouseMove);
while 1
loc = get(gca, 'CurrentPoint');
plot(loc(1), loc(2), 'r.', 'markersize', 15);
pause(.03);
end
However, even this only plots according to the mouse's x position (it plots a straight line, with both the x and y values of that line being equal to the mouse's relative x position). So I'm halfway there, but I'm not sure why the y value is equal to the x value all the time.
  1 Comment
Walter Roberson
Walter Roberson on 29 Jan 2017
Note: Callbacks can only return values when the callbacks are used for filtering, such as position constraint "callbacks".

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Jan 2017
plot(loc(1,1), loc(1,2), 'r.', 'markersize', 15);

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!