, ascii. , ,
function progressbar(percent, N, init, extrastr)
% Draws a progress bar in the matlab command prompt. Useful for lengthly
% calculations using for loops
%
% Arguments:
% - percent: A number between 0 and 1
% - N: how many characters wide the bar should be
% - init: (optional; default false) true or false; whether or not
% this is the first time calling the progressbar function for
% your current bar.
% - extrastr: (optional; default char(10)) An extra string to append to
% the progress bar. Things will go screwy at the command
% console if this string changes length from call to call of
% progressbar.
%
% Outputs:
%
% Usage Example:
%
% for k=1:1000
% progressbar(k/1000,50,k==1,sprintf('\n We are are on number%4d\n', k));
% % fake a computation
% pause(0.05);
% end
%
if nargin < 3
init = 0;
end
if nargin < 4
extrastr = char(10);
end
percent = min(max(real(percent),0),1);
done = round(N*percent);
done_str = '*'*ones(1, done);
left_str = '-'*ones(1, N-done);
bar = sprintf(['||' done_str left_str '|| %3d'],round(percent*100));
erase = [];
if ~init
% use backspace characters to erase the previously drawn bar
erase = ['' char(8)*ones(1,length(bar)+length(extrastr)+1)];
end
fprintf([erase bar '%s' extrastr], '%');
drawnow;
end
for , , , 100 , .