Requesting Functions too quickly with sliders in GUI???

4 views (last 30 days)
I designed some sliders that adjust the brightness of an image. The image is first shown on a figure and that figure is assigned a handle. After that, whenever the user changes the value of the slider, the new brightness of the image is calculated, the figure handle is called and the updated image is displayed on the figure. However, if I press the sliders too quickly, then my button group windows disappear and a new figure opens up with the original image, the old one stays, and everything seems to kinda crash.
It seems like pressing the button too quickly is requesting a new function before the old one is completed. Is there any way to stop the user from changing the slider until the new image has been fully updated and shown??? I have tried stuff with 'drawnow nocallbacks','pause', and waitfor(but what would I delete for waitfor). Maybe I'm not doing it correctly. What should I do to make sure that the user cannot change the slider until the previous function action is fully completed???

Answers (1)

Walter Roberson
Walter Roberson on 16 Oct 2017
Typically when you see behaviour like that, you have failed to "parent" some graphics call. "parenting" a graphics call means to ensure that for every graphics call that creates a new object, you tell it specifically which object is to be the parent object, and that for any call that relies upon a "current" object, that you instead specifically tell it which object to operate on.
For example, instead of code that looks like this, starting from no graphics:
plot(1:10, 15:24)
box on
you would use
fig = figure();
ax = axes('Parent', fig);
plot(ax, 1:10, 15:24);
box(ax, 'on')
The parent for the plot() was to be provided, which required that the axes was created, so the parent for the axes should be created first. Then afterwards, the axes to be worked on by "box" was named directly.
Doing this kind of parenting of every graphics call ensures that the graphics is always going to affect exactly what you want, with there being no possibility that a callback at the "wrong" time could change the focus, and no possibility that accidentally clicking in the wrong place could change the focus -- and no possibility that when you are debugging that you might touch a window to move out out of the way for debugging and so accidentally give it focus when you did not want that.
But if you do not do that, then one thing to do is to ensure that you do not drawnow() or pause() until you are finished all the processing steps for a callback. Every drawnow() or pause() allows a chance for the graphics event queue to be served, and so allows another invocation of the callback to run (and make changes to variables!) before you might really have finished. You can reduce the effects of this by setting the object Interruptible property and BusyAction property.

Categories

Find more on Graphics Performance 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!