在MATLAB中,如何在拖动滑块时执行回调? [英] In MATLAB, how can you have a callback execute while a slider is being dragged?

查看:1078
本文介绍了在MATLAB中,如何在拖动滑块时执行回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用GUIDE创建了一个MATLAB GUI。我有一个带有回调函数的滑块。我注意到这个回调,它应该执行滑块移动,实际上只有一旦滑块已经移动和鼠标释放后运行。

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 fact only runs once the slider has been moved and the mouse released.

有一种方法来获取脚本在滑块被拖动时运行,用于实时更新图表?

Is there a way to get a script to run as the slider is being dragged, for live updating of a plot? There would I presume need to be something to stop the script being run too many times.

推荐答案

尽管滑块的回调在鼠标移动时未被调用, 'Value'属性的滑块uicontrol 正在更新。因此,您可以使用 ADDLISTENER 创建侦听器,该侦听器将在执行回调时使用'Value'属性更改。例如:

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'));

当您移动滑块时,应该会看到'hi'打印到屏幕(监听器回调),当你释放鼠标时,你将看到打印的'hello'(uicontrol回调)。

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

这篇关于在MATLAB中,如何在拖动滑块时执行回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆