State Change Callback on Slider Causing Error if Changed Too Fast

3 views (last 30 days)
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?

Answers (1)

Rik
Rik on 1 Feb 2018
Edited: Rik on 2 Feb 2018
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 Comments
Jeremy
Jeremy on 2 Feb 2018
Edited: Jeremy on 2 Feb 2018
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.
Rik
Rik on 2 Feb 2018
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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!