How do I continuously read the mouse position as the mouse is moving, without a click event?

477 views (last 30 days)
I want to read the (x, y) position of the mouse continuously as the mouse moves around the figure, without the need for me to explicitly click at a particular position to use the GINPUT or an equivalent function.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
It is possible to record the mouse position continuously in MATLAB without the need for mouse-click events to occur. The current cursor (mouse) position on a figure window can be read by using the 'CurrentPoint' property of the figure. Since you need this mouse position to be read each time the mouse moves, you would assign a callback function to the 'WindowButtonMotionFcn' property of the figure window. This callback function would then read this 'CurrentPoint' property mentioned above.
For example, open a new figure window at the MATLAB prompt. Now, we set the 'WindowButtonMotionFcn' property to point to the callback function which will execute each time the mouse moves on the figure. We will create this callback function in a bit, but for now we will call it 'mouseMove'. The following statement assigns this callback function.
set (gcf,'WindowButtonMotionFcn', @mouseMove);
Now we create this callback function in the MATLAB editor by typing 'edit mouseMove' at the MATLAB command prompt. An example of the callback function is shown below but in your function, you should include the steps that you want to take when the mouse moves.
functionmouseMove (object, eventdata)
C = get (gca,'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)),', ',num2str(C(1,2)),')']);
Save this function in the path and then move your mouse over the figure. The mouse position will continuously be printed in the title of the axes.
11 Comments
Barbara Kammerl
Barbara Kammerl 2021年4月15日
I'm relatively new to MATLAB, but i managed to get the mouse position in App Designer after trial and errors, but mostly thanks to this thread: https://de.mathworks.com/matlabcentral/answers/603649-how-to-get-mouse-coordinates-in-gui-appdesigner
It doesn't work in an Axis-Field but you can get the general position of the mouse in the whole app window. Maybe you can calculate the mouse position whith this and the general Position of the Axis-Field. The 0-Position of the x and y axis is the bottom left corner.
I put this as a new function:
properties (Access = private)
x = 0% x position
y = 0% y position
end
methods (Access = private)
functionfunc(app, axes, event)
app.x = axes.CurrentPoint(1,1);% to access x and y outside the callback
app.y = axes.CurrentPoint(1,2);
app.xEditField.Value = app.x;%Puts the x-value in numeric field "x"
app.yEditField.Value = app.y;%Puts the y-value in numeric field "y"
end
end
And added this to the startupFcn(app):
app.UIFigure.WindowButtonMotionFcn = @app.func;
I hope this helps someone.

Sign in to comment.

More Answers (1)

raym
raym on 24 Mar 2017
Edited:MathWorks Support Team on 24 Jun 2021
edited Jan 7 '15 at 17:07 answered Jan 7 '15 at 7:38 Are you sure MATLAB can only get the mouse co-ordinates within a GUI? It's actually quite simple to get the position of your mouse anywhere on your screen, independent of a GUI.
Use the following:
get(0, 'PointerLocation') Try this by moving your mouse around and invoking this command each time. You will see that the output keeps changing when the mouse moves. You'll see that this works independent of a GUI.
The output of this function will return a two-element array where the first element is the x or column position and the second element is the y or row position of your mouse. Bear in mind that the reference point is with respect to the bottom left corner of your screen. As such, placing your mouse at the bottom left corner of the screen should yield (1,1) while placing your mouse at the top right corner of the screen yields the resolution of your screen.
Now, if you want to continuously get the position of the mouse, consider placing this call in a while loop while pausing for a small amount of time so you don't overload the CPU. Therefore, do something like this:
while condition loc = get(0, 'CurrentPosition');
%// Do something
%...
%...
pause(0.01);%// Pause for 0.01 ms
end

下载188bet金宝搏

Community Treasure Hunt

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

Start Hunting!