我使用指南创建了一个Matlab GUI。我有一个带回调函数的滑块。我注意到这种应该在滑块运动上执行'在滑块运动'的回调中,实际上只运行滑块已移动并释放鼠标。

是否有一种方法可以在被拖动的滑块被拖动时获取脚本运行,用于绘图的实时更新?我会发现需要停止脚本的东西太多时间。

有帮助吗?

解决方案

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).

其他提示

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.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top