我正在使用 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.
由于您有循环,因此可以插入 pause
或 drawnow
命令以允许 MATLAB 处理其他事件,例如鼠标单击其他按钮 (暂停(0)
可能会起作用 - 尚未测试 - 允许检查中断事件,而不会在没有中断的情况下实际导致循环变慢)。
(旁注:ctrl-c 会跳出循环,因此您始终可以这样做,但......并不理想。)
关于matlab - Matlab GUI 中的无限循环导致 Matlab 在 GUI 关闭时卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532175/