【问题标题】:Abort current script on closing waitbar在关闭等待栏时中止当前脚本
【发布时间】:2013-01-23 17:16:03
【问题描述】:

我正在使用这样的等待栏:

h = waitbar(0,'Please wait...');
for i=1:100, % computation here %
   waitbar(i/100)
   % other operation here
end
close(h) 

如果用户关闭等待栏(单击窗口的X),我想停止此脚本,而无需添加取消按钮。

有什么办法吗?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    您可以测试h 是否为有效句柄,否则退出循环。在循环中插入以下内容:

    if ~ishandle(h)
        break
    end
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      if ishandle(h),
         close(h);
         % Your code here
      else
          %waitbar has been closed by the user
          % call throw, return, or break
      end
      

      希望对你有帮助,

      【讨论】:

      • close(h) 关闭等待栏而不是脚本
      • 是的,代码在意义上是不完整的,你基本上检查等待栏是否关闭。您可以使用 returnbreakthrow 语句来跟进,具体取决于您使用它的位置
      • @KiranChandrashekhar:我看不出这会对用户关闭的窗口有何反应 - 您必须检查句柄是否不再存在。
      • @KiranChandrashekhar:那你应该写if ~ishandle(h)
      • 嗨,答案并不完整。这个想法是检查等待栏是否已被用户关闭。我正在更新答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多