State Change Callback on Slider Causing Error if Changed Too Fast

I have a jslider with a state change callback. When I drag the slider thumb, code is executed to calculate an area under a curve using trapz. The slider controls the end point of the integral interval. If I scroll through the slider slowly, or even at a reasonable pace, the code runs fine and there are no errors. But if I drag the slider quickly, I get an error with the trapz function that makes me think the code is caught in an unfinished state from the previous state change and is having a hard time keeping up with the slider value changing. I've thought about changing the callback to a mouse released or something that wouldn't change continuously, but I like the value given in real time as the slider is dragged. Any suggestions?

回答(1 个)

Include a flag in your handles struct that returns the callback if there is already a callback being executed.
function your_slider_callback(hObject,eventdata)
handles=guidata(hObject);
if handles.isbusy
return
end
handles.isbusy=true;
guidata(hObject,handles)
%rest of your callback function%
handles.isbusy=false;
guidata(hObject,handles)
end

6 个评论

I tried your suggestion and I get the following error:
"Not enough input arguments" on the last line of the following code
function stopSlider_Callback(hObject, eventdata, handles )
if handles.isbusy
In that case your callback doesn't automatically load the handles, so you'll have to do that yourself. I'll edit my answer.
Now I get the following error after the edit:
Undefined function or variable 'toDouble'.
Error in opaque/double (line 79)
dbl = toDouble(opaque_array(1));
Error in guidata (line 83)
if isscalar(h) && ishghandle(h)
Error in Moldy_v3/stopSlider_Callback (line 278)
handles = guidata(hObject);
I don't understand why this error occurs. What version of Matlab are you using? And on what OS?
I think it has to do with the fact that I'm using a java javax.swing.JSlider. I found this article which discusses the toDouble function that is at the root of the error. I'm not sure why I didn't get any errors before the code you suggested, though. Most of what is discussed in the article is over my head.
What you can try is gcbf or even gcf as arguments to guidata, although I haven't worked with Java objects enough to understand your program structure.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品

提问:

2018-2-1

评论:

Rik
2018-2-2

Community Treasure Hunt

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

Start Hunting!

Translated by