Waitbar导致许多数字打开 - MATLAB

时间:2014-07-21 15:05:26

标签: matlab

所以,我创建了一个MATLAB脚本,需要几分钟才能完成。我决定在这里添加一个与文档相对应的等待栏:http://www.mathworks.se/help/matlab/ref/waitbar.html

我已将其定义如下:

function crazyfunction4you();

h = waitbar(0,'Setting up...')

for i =1:N
    waitbar(i/N,'Loading...')
 %Some calculations is going on here

end

close(h);

end

不知何故,这是打开一个数字的数字,最终导致我的机器崩溃。我发现这种奇怪,因为我希望只出现一个数字:

enter image description here

我有兴趣听听你们有类似经历的天气吗?

2 个答案:

答案 0 :(得分:5)

您需要将句柄包含在等待栏中,否则Matlab会解释您要创建另一个句柄:

h = waitbar(0,'Setting up...')
for i =1:N
    waitbar(i/N,h,'Loading...') %// Only this line changed. Added a handle
                                %// to refer to previously created waitbar
    %Some calculations is going on here
end
close(h);

答案 1 :(得分:2)

除了Luis Mendo的回答之外,您可能还想考虑等待条的更新频率,因为如果N非常大,那么在每次循环迭代中更新等待条都会增加一些荒谬的开销

要解决这个问题,请执行

之类的操作
k = 0.1; % // determines how often bar is updated, 0.1 means every 10%, 0.05 means
         % // every 5% etc.

h = waitbar(0,'Setting up...')
for i =1:N
    if mod(i/N,k)==0                % // added to supress unnecessary waitbar updates
        waitbar(i/N,h,'Loading...')
    end                        
    %Some calculations is going on here
end
close(h);