matlab for loop 捕获并跳过错误+并行+进度条

for k = ...
    try
        your code here
    catch
        fprintf('loop number %d failed\n',k)
    end
end

ref : How do I skip an error in a loop? - MATLAB Answers - MATLAB Central (mathworks.cn)

%%% use parallel compute
% f = waitbar(0, 'Starting');
n = length(file_list);
parpool(10); % 确定使用的核心数量
% Then construct a ParforProgressbar object:
ppb = ParforProgressbar(n);

parfor i = 1:n
    try
    
    
    %%% input your code

    catch 
        fprintf('loop number %d failed\n',k)
    end
    % waitbar(i/n, f, sprintf('Progress: %d %%', floor(i/n*100)));
    ppb.increment();
end
delete(ppb);  % 删除进度条
delete(gcp('nocreate'))  %关闭当前并行池

ref: PARFOR progress monitor (progress bar) v4 - File Exchange - MATLAB Central (mathworks.cn)

注意该脚本无法在matlab 的交互式脚本内运行
解决办法见上面网页里面的评论区:

a very simple fix is to add "'Visible', 'on'" to the figure options when creating the figure (line 196 "progressbar.m").

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容