How to implement a 'speed limit' for GUI button press?

Hi, I have a GUI built to flip through slices of large 3D image matrices and adjust visualization parameters. When changing slices with a slider, if the slider is clicked too fast in succession the GUI lags a lot because it is trying to render each intermediate image. I want to add a button press 'speed limit' such that: if slider clicks > 1 && time < threshold, skip displaying the intermediate images and just generate display for the final slider setting, else, refresh the image for each slider click, end
I don't know what functions I could use to implement that, as it's not really a timeout. Schemes I can think of using tic, toc or uiwait would require a second click which may not be coming. Any ideas would be appreciated! Thanks!

1 Kommentar

John
John am 19 Apr. 2011
Thanks to all for the suggestions. While Matt's answer works, it forces the user to wait between clicks whereas Paulo's answer allows the user to click at will and provides them with a smoother experience regardless. Also, I found that adding 'drawnow' at the end of the image generation code made that function a lot more responsive, which alone fixed the problem for small to moderately sized data sets.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Paulo Silva
Paulo Silva am 19 Apr. 2011

1 Stimme

Use a timer (execution mode 'singleShot') in the slider callback:
  1. Check if the timer is running, if not start it and run the slider code
  2. If the timer is already running ignore all the rest of the slider code

1 Kommentar

Good answer.
You could also have the timer disable the slider until it's done.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Matt Fig
Matt Fig am 19 Apr. 2011

1 Stimme

Just set the 'enable' property to 'inactive' as the first command of the callback, then as the last command, set the 'enable' property to 'on'.
Malcolm Lidierth
Malcolm Lidierth am 19 Apr. 2011

0 Stimmen

Change the callback mode to to 'cancel' instead of 'queue'. Alternatively, test for multiple calls in the callback using isMultipleCall from the FEX. if isMultipleCall();return;end http://www.mathworks.com/matlabcentral/fileexchange/26027

4 Kommentare

Matt Fig
Matt Fig am 19 Apr. 2011
I thought of this too, but apparently 'busyaction' doesn't work like that. I don't know why, but in 2007b, busyaction set to cancel behaves exactly as if set to queue.
Matt Fig
Matt Fig am 19 Apr. 2011
Here is an example of how this does not work in 2007b.
uicontrol('style','slider',...
'callback','disp(''1''),sort(rand(1,5e6));disp(''2'')',...
'interrupt','off',...
'busyaction','cancel');
Clicking 3 times fast on the right arrow, we should only see one set of numbers, because the busyaction is set to cancel and the interruptable property is set to off. I see three sets of numbers....
Another reason to use isMultipleCall?
Yair Altman
Yair Altman am 21 Jul. 2011
Matt - if you add even a short pause(0.1) in your callback string then the busyaction=cancel does indeed run only once, not 3 times. My hunch is that what happens under the hood is that the callback is actually delegated to a separate thread (maybe EDT delegating to the MT?). This delegation happens so quickly that if you double-click the separate clicks do not in fact overlap, and both callbacks will then get executed one after another. However, when you add a pause, it forces the interpreter to use the original thread (instead of delegating), and in this case the clicks *do* overlap and so the second click is discarded.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by