How can I compress the button motion events?

3 views (last 30 days)
Naum Derzhi
Naum Derzhi on 9 Jun 2021
Answered: Swastik on 28 Feb 2024
I attach a callback to WindowButtonMotion event to track the cursor location. The code is doing some stuff in response to mouse movement, and the events are quiing up faster than the code can react tot them. I need to discard the events accumulated while the callback was executed and pickup only the next event occuring after the callback is done. In some systems this is called "event compression". However, I did not find anything similar in Matlab.
Any ideas will be appreciated.
I can, of course, detach the callback until it's done and then reattach it, but this seems to be inelegant. Are there any better solutions?
Thank you

Answers (1)

Swastik
Swastik on 28 Feb 2024
It seems you are looking to apply Event Compression within a MATLAB figure, aiming to ensure that only the most recent event is processed while discarding others, much like a mutex mechanism in programming.
Here is a MATLAB code example that initiates a MATLAB figure which you can build upon to incorporate event compression:
f = figure();
f.WindowButtonMotionFcn = @(fig, eve) btnCb(fig);
f.UserData.isBusy = false;
function btnCb(fig)
if fig.UserData.isBusy
return;
end
fig.UserData.isBusy = true;
currentPoint = get(fig, 'CurrentPoint');
disp(num2str(currentPoint(1,2)) + ", " + num2str(currentPoint(1,2)));
pause(1); % Simulation of high compute task.
fig.UserData.isBusy = false;
end
I hope this serves as a helpful starting point for your implementation.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!