function [] = progress_bar(idx, min, max, message, GUI)
% function [] = progress(idx, min, max, [message], [GUI])
%
% Displays a text progress bar when contained inside a for loop.
% idx The index variable of the for loop.
% min The minimal value of idx.
% max The maximal value of idx.
% [message] A message to display to the user when the progress function is
% first called.
% [GUI] If true, the progress will be shown in a pop up window
% including an estimated time remaining. This is slightly slower
% (?5s).
%
% Example:
% >> for idx = 1:100
% progress(idx, 1, 100, 'testing the progress bar!');
% pause(0.05);
% end
persistent handle;
if nargin < 5, GUI = false; end
if (GUI)
if (idx == min)
handle = waitbar(0, 'starting up');
if nargin >= 4
set(handle,'Name',message);
end
tic;
else
frac = (idx - min) / (max - min);
ETR = round(toc * (1 - frac) / frac);
waitbar(frac, handle, strcat('time remaining: ', num2str(ETR), ' s\newline', num2str(floor(100*frac)), '%'));
if (idx == max)
delete(handle);
end
end
else
if nargin < 4, message = 'starting iterative process, progression:'; end
fracs = 0.01:0.01:1; % fractions to print
if (idx == min)
fprintf(message);
fprintf('\n');
fprintf('0%% 10%% 20%% 30%% 40%% 50%% 60%% 70%% 80%% 90%% 100%%\n');
fprintf('v---------v---------v---------v---------v---------v---------v---------v---------v---------v---------v\n');
fprintf('|');
else
f0 = (idx - 1 - min) / (max - min); % fraction of iterations completed last step
f1 = (idx - min) / (max - min); % fraction of iterations completed
select = (f1 >= fracs) & ~(f0 >= fracs); % The index vector of the fracs to print
for idx2 = 1:sum(select)
fprintf('|');
end
if (idx == max)
fprintf('\n');
end
end
end
return