使用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
使用中的问题
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
经过检查,发现是和
datetime
数据类型有关最终发现是,string(time(i))
耗费了很长时间,修改掉即可