matlab - Matlab GUI 中的无限循环导致 Matlab 在 GUI 关闭时卡住?

标签 matlab user-interface infinite-loop matlab-guide

我正在使用 GUIDE 在 Matlab 中创建 GUI。当用户点击 GUI 中的“开始”按钮时,它会在后台启动无限循环运行的优化任务。每次迭代都会向 GUI 输出一些信息。

当我按下开始按钮然后关闭 GUI 窗口时,Matlab 卡住。当我运行 GUI 但不按“开始”按钮并只是关闭 GUI 时,它不会卡住。

如何避免卡住?

最佳答案

您启动的无限循环不允许处理任何进一步的事件(即窗口关闭事件)。您需要允许 interrupt发生机制 - 尽管'interruptible'属性默认为'on',但您必须满足另一个要求:

If the Interruptible property of the object whose callback is executing is on, the callback can be interrupted. However, it is interrupted only when it, or a function it triggers, calls drawnow, figure, getframe, pause, or waitfor. Before performing their defined tasks, these functions process any events in the event queue, including any waiting callbacks. If the executing callback, or a function it triggers, calls none of these functions, it cannot be interrupted regardless of the value of its object's Interruptible property.

由于您有循环,因此可以插入 pausedrawnow 命令以允许 MATLAB 处理其他事件,例如鼠标单击其他按钮 (暂停(0)可能会起作用 - 尚未测试 - 允许检查中断事件,而不会在没有中断的情况下实际导致循环变慢)。

(旁注:ctrl-c 会跳出循环,因此您始终可以这样做,但......并不理想。)

关于matlab - Matlab GUI 中的无限循环导致 Matlab 在 GUI 关闭时卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532175/

相关文章:

matlab - 将二维矩阵与向量相乘以跨越第三维 - MATLAB

matlab获取实际值 'xTick'

matlab - 以完全矢量化的方式用前一个元素逐行或逐列替换矩阵中的零(或 NAN)

java - Swing 中的注音假名(或 GUI 替代品……)

java - 如何使用 Scanner 处理无效输入(InputMismatchException)引起的无限循环

matlab - 为什么 MATLAB 会删除我的 classdef 中的断点?

java - 我正在制作一个游戏,我需要它在 java Guy 中每次输或赢时将其加起来

delphi - 对 GUI 元素进行排序而不是对数据进行排序并让 GUI 显示排序后的数据是否更可取?

JavaScript 99Bottles - 无限循环

java while 循环没有中断