在 MATLAB 中,如何在拖动滑块时执行回调?

发布于 2024-11-07 18:53:10 字数 143 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风无影 2024-11-14 18:53:10

即使鼠标移动时不会调用滑块的回调,滑块 uicontrol 的'Value' 属性正在正在更新。因此,您可以使用 addlistener 创建监听器 将在 'Value' 属性更改时执行给定的回调。下面是一个示例:

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

当您移动滑块时,您应该看到 '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 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).

孤城病女 2024-11-14 18:53:10

仅供记录,此主题在此详细讨论: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 the handle.listener alternative, since addlistener is basically just a wrapper for the latter.

故人爱我别走 2024-11-14 18:53:10

如果您想执行传递给 uicontrol 的相同原始回调,您可以添加此通用侦听器来引导现有回调:

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

相关博客文章

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文