2013-01-23 36 views
6

我使用這樣的waitbar:中止當前的腳本在關閉waitbar

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

我想如果用戶關閉waitbar停止此腳本(點擊窗口X),而無需添加一個取消按鈕。

有沒有辦法做到這一點?

回答

5

您可以測試h是否爲有效句柄,否則退出循環。將以下到您的循環:

if ~ishandle(h) 
    break 
end 
1

你可以嘗試這樣的事情:

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

希望它能幫助,

+0

接近(H)關閉waitbar不是腳本 – dynamic

+0

是,代碼在這個意義上不完整,你基本上檢查waitbar是否關閉。你用一個'return'或者一個'break'或者'throw'聲明,根據你使用它的位置進行跟蹤。 – Kiran

+0

@KiranChandrashekhar:我沒有看到這將如何對用戶關閉的窗口做出反應 - 你必須檢查句柄是否不存在。 – Jonas