- 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 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Ninad
2024년 7월 4일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!