在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使鼠标移动时不会调用滑块的回调,滑块 uicontrol 的
'Value'
属性正在正在更新。因此,您可以使用addlistener
创建监听器 将在'Value'
属性更改时执行给定的回调。下面是一个示例:当您移动滑块时,您应该看到
'hi'
被打印到屏幕上(侦听器回调),当您释放鼠标时,您将看到'hello'
code> 打印(uicontrol 回调)。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 usingaddlistener
that will execute a given callback when the'Value'
property changes. Here's an example: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).仅供记录,此主题在此详细讨论:http://UndocumentedMatlab.com/blog /continuous-slider-callback/ - 那里提出了几种替代解决方案。 gnovice 使用
addlistener
的解决方案相当于handle.listener 替代方案
,因为addlistener
基本上只是后者的包装器。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 thehandle.listener alternative
, sinceaddlistener
is basically just a wrapper for the latter.如果您想执行传递给 uicontrol 的相同原始回调,您可以添加此通用侦听器来引导现有回调:
相关博客文章
If you want to execute the same original callback you passed to
uicontrol
you can add this generic listener which bootstraps the existing callback:Related blog post