In MATLAB, how can you have a callback execute while a slider is being dragged?

前端 未结 3 1944
萌比男神i
萌比男神i 2020-12-06 06:12

I have created a MATLAB GUI using GUIDE. I have a slider with a callback function. I have noticed that this callback, which is supposed to execute \'on slider movement\', in

相关标签:
3条回答
  • 2020-12-06 06:52

    Just for the record, this subject is discussed in detail here: http://UndocumentedMatlab.com/blog/continuous-slider-callback/ - several alternative solutions are presented there. gnovice's solution using addlistener is equivalent to the handle.listener alternative, since addlistener is basically just a wrapper for the latter.

    0 讨论(0)
  • 2020-12-06 07:14

    Even though the callback of the slider isn't being called as the mouse is moved, the 'Value' property of the slider uicontrol is being updated. Therefore, you could create a listener using addlistener that will execute a given callback when the 'Value' property changes. Here's an example:

    hSlider = uicontrol('Style', 'slider', 'Callback', @(s, e) disp('hello'));
    hListener = addlistener(hSlider, 'Value', 'PostSet', @(s, e) disp('hi'));
    

    As you move the slider you should see 'hi' being printed to the screen (the listener callback), and when you release the mouse you will see 'hello' printed (the uicontrol callback).

    0 讨论(0)
  • 2020-12-06 07:15

    If you want to execute the same original callback you passed to uicontrol you can add this generic listener which bootstraps the existing callback:

    sld.addlistener('Value','PostSet',@(src,data) data.AffectedObject.Callback(data.AffectedObject,struct('Source',data.AffectedObject,'EventName','Action')));
    

    Related blog post

    0 讨论(0)
提交回复
热议问题