Skip to main content

MATLAB: add progress bar in your code






Function_Progress
function Function_Progress(ti,day,len)

%ti = cputime; put above for loop
figure(1000)
per=min(1+day*100/len,100);per2=day*100/len;
b2 = barh(100,'FaceColor',[0.8,0.8,0.8]);b2.BarWidth= 0.11;
hold on;clr = interp1(linspace(1,101,100),winter(100),per);
b = barh(per,'FaceColor',clr);
xlim([0 100]);
box on;b.BarWidth= 0.1;
set(gca,'YTick',[]);set(gca,'XTickLabel',[]);set(gca,'XTick',[]);
txt1 = sprintf('complete = %.0f %%',per2);t=text(34,1.2,txt1);t.FontSize = 14;
e = (cputime-ti);txt2 = sprintf('Time Taken: %.0f:%.0f',e/60,rem(e,60));t2=text(67,0.55,txt2);t2.FontSize = 10;
c = clock;txt3 = sprintf('Time Now: %.0f:%.0f',c(4),c(5));t3=text(0.5,0.55,txt3);t3.FontSize = 10;
hold off;
getframe(gcf);

% hold on; b3= barh(100,'g');b3.BarWidth= 0.1; put below for loop

end

Run_progress
t = cputime;
for i=1:100
Function_Progress(t,i,100)
end
hold on; b3= barh(100,'g');b3.BarWidth= 0.1;
Untitled
%% **************** OR ****************
h = waitbar(0,'Im progressing');
TT=120;
for t = 1:TT
% Update waitbar
waitbar(t/TT)
end

Untitled
%% **************** OR ****************
for now=1:1:100
Total=100;progressbar(Total,now)
end
| | 1 % |= | 6 % |== | 11 % |=== | 16 % |==== | 21 % |===== | 26 % |====== | 31 % |======= | 36 % |======== | 41 % |========= | 46 % |========== | 51 % |=========== | 56 % |============ | 61 % |============= | 66 % |============== | 71 % |=============== | 76 % |================ | 81 % |================= | 86 % |================== | 91 % |=================== | 96 % |====================| 100 %
function progressbar(Total,now)
if nargin<2;Total=100;now=10;end
da=[1:round(Total/20):Total,Total];
if sum(da==now)>0
a={ '| |',...
'|= |','|== |',...
'|=== |','|==== |',...
'|===== |','|====== |',...
'|======= |','|======== |',...
'|========= |','|========== |',...
'|=========== |','|============ |',...
'|============= |','|============== |',...
'|=============== |','|================ |',...
'|================= |','|================== |',...
'|=================== |','|====================|'};
loc=floor((now*20/(Total)))+1;
if loc<=0;loc=1;end
if loc>20;loc=21;end
%%
progress=sprintf('%s %0.00f %%',a{loc},round(now*100/Total));
disp(progress)
end
end