Hi Rukhsar
You can use 'get(groot, "PointerLocation") to get the current location of the cursor. Consider the following code for creating a live plot of cursor movements:
fig = figure('Name', 'Mouse Position Tracker');
title('X-Coordinate vs Time');
xlabel('Time (seconds)');
ylabel('X Position (pixels)');
title('Y-Coordinate vs Time');
xlabel('Time (seconds)');
ylabel('Y Position (pixels)');
currentPos = get(groot, 'PointerLocation');
current_time = toc(start_time);
x_positions = [x_positions currentPos(1)];
y_positions = [y_positions currentPos(2)];
timestamps = [timestamps current_time];
set(h1, 'XData', timestamps, 'YData', x_positions);
set(h2, 'XData', timestamps, 'YData', y_positions);
xlim([max(0, current_time-10) current_time+0.1]);
xlim([max(0, current_time-10) current_time+0.1]);
This code creates a window with two graphs that show your cursor's X and Y positions over time. The top graph (blue line) tracks horizontal movement, and the bottom graph (red line) tracks vertical movement. It updates every second and shows the last 10 seconds of movement. The tracking continues until you close the window.
Here is the output of the above code:
For more information, refer to the following documentations:
- groot - https://mathworks.com/help/matlab/ref/groot.html
- get - https://mathworks.com/help/matlab/ref/get.html
Hope this helps!