在Command Window显示进度条

使用waitbar函数显示循环中的进度

waitbar(0)
for i=1:1000000
    waitbar(i/1000000)
end

当循环很多时,速度就会大幅下降,应该是使用GUI刷新的问题

自己写一个progress_bar函数

function [] = progress_bar(i,n,step, str)
% PROGRESS_BAR can show a progressbar for your code
%
% i    : changed number in the loop
% n    : number of the loop
% step : control progress bar refresh frequency;The larger the number, the faster the refresh
% str  : explanatory text
%
% Example code:
% for i=1:100000000
%     progress_bar(i,100000000,50, "已完成");
% end
resolution = n/step;
num=i/resolution;
t=floor(num)*resolution;
if i-t==0
    clc;
    num_max = n/resolution;
    all_char = 50;
    t=num/num_max*all_char;
    num_sharp=floor(t);
    if num_sharp<=all_char/2
        disp(['  ' char(str) ' :  ' repelem('#',num_sharp) repelem('-',all_char/2-num_sharp) '  ' char(string(vpa(t/all_char*100,2))) '%  ' repelem('-',all_char/2)]);
    else
        disp(['  ' char(str) ' :  ' repelem('#',all_char/2) '  ' char(string(vpa(t/all_char*100,2))) '%  ' repelem('#',num_sharp-all_char/2) repelem('-',all_char-num_sharp)]);
    end
end
end

for i=1:100000000
    progress_bar(i,100000000,50, "已完成");
end
progress_bar演示.gif

使用中的问题

for i=1:length(time_need)
    for m = 1:length(ny)
        for n=1:length(nx)
            id = n+(m-1)*length(nx);
            points(id,:,i)= [id, lon(n), lat(m), extractedGrids(n,m,i)];
            progress_bar(id,length(nx)*length(ny),50,"格网"+string(time(i))+": ");
        end
    end
    out(i).points = points(:,:,i);
    out(i).time = extractedTime(i);
    out(i).time_bounds = extractedTimeBounds(:,i);
end

实际使用时很慢.gif

经过检查,发现是和datetime数据类型有关
image.png

最终发现是,string(time(i))耗费了很长时间,修改掉即可

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

推荐阅读更多精彩内容