- The first argument (0) refers to the root object (the entire screen), and the second argument ('PointerLocation') specifies the property we want to retrieve.
- The property 'PointerLocation' returns a 2-element vector [x, y], where x and y represent the horizontal and vertical coordinates of the mouse pointer, respectively.
How to deal with timing error when using Timer Function?
4 views (last 30 days)
Show older comments
Hi everyone,
I am currently writing a code using Timer function to record mouse path with 1kHz rate (i.e. I want to record the position of the mouse every 0.001s). I discovered that the Timer function does not have the precision under 50ms. I also found that the matlab time outputs did not add up to the total trial running time (i.e. if I add up the time tracked by the Timer function, the value is always less than the actual running time of the mouse path). I think if the error in timing comes from the accumulation of excution time of the Timer function, in theory the value should be greater than the actual running time. I am hoping if anyone could take a look at my code and give me some suggestion, and am also wondering if people have encountered similar issue. The following is the Timer Function that I used:
timerObj = timer('TimerFcn', @recordMousePos, 'Period', 0.001, 'ExecutionMode', 'fixedRate');
Thank you!
0 Comments
Answers (1)
Ninad
on 4 Jul 2024
Hi Sicheng,
You can use the "get" MATLAB function to achieve your goal. This MATLAB function retrieves the current position of the mouse pointer (cursor) on the screen.
In the following command:
loc = get(0, 'PointerLocation');
I have written a dummy code where you can use the get function to track the cursor on the screen:
flag = 0;
while true
loc = get(0, 'PointerLocation');
if (flag==0)
prev_locx = loc(1);
prev_locy = loc(2);
flag=1;
end
new_locx = loc(1);
new_locy = loc(2);
if (new_locy~=prev_locy) || (new_locx~=prev_locx)
fprintf('Mouse position: X = %d, Y = %d\n', loc(1), loc(2));
prev_locx = new_locx;
prev_locy = new_locy;
end
pause(0.001); % Pause for 1 ms (adjust as needed)
end
Please go through the following MathWorks documentation to know more about the "get" function:
Regards,
Ninad
See Also
Categories
Find more on Startup and Shutdown 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!