Simple script that displays the percentage of a loop that has been completed in a running loop. The user can define the increment percentage and a title of the loop.
Note this function is also useful in nested loops by having itot=itot1*itot2*itot3 e.g. for a nest of 3 loops.
This is great. It's a sleek little feature that is often useful to estimate how far along your scripts are. I've been putting in low values of incrementations to display for initial debugging, then higher values (1%) for running scripts regularly.
I added in a time estimate for the completion of the loop:
%PRCDONE calculate the percentage of loop done in a running loop and
% display an estimate of remaining run time
% PRCDONE(ILOOP,ITOT,DPRC) convert iloop into iloop/itot*100% and display
% the result by dprc incrementations
% ILOOP is the current loop id (assumes iloop = 1:itot)
% ITOT is the total number of iterations
% TITLE (string, optional) is the title of the loop for display
% If using dprc set title to '' if you don't want a title
% DPRC (optional) is the incrementations to display (in %).
% Default is 10%
% TSTART is the stopwatch time just before the loop is entered. Use e.g. "tstart
% = tic"; just above your loop. This function uses toc to determine
% the elapsed time.
% Warning: this script will add time on your loop
% for i0 = 1:100
% Original Author: Arnaud Laurent
% Creation : Dec 4th 2012
% Updates: Matthew Gruber
% Nov 1st 2017
% MATLAB version: R2015a
% Last modified: November 11th 2017
title = ' unnamed ';
frac_now = dprc*(ceil(iloop/itot*100/dprc));
frac_next = dprc*(ceil((iloop+1)/itot*100/dprc));
if iloop == 1
disp(['Starting ' title ' loop'])
disp(['Done ' title ' loop'])
disp([num2str(frac_now) '% completed of "' title '" loop.'])
disp('Unable to estimate a completion time without a start time input.')
rate=iloop/toc(tstart); % iterations per second
i_to_do=itot-iloop; % iterations
timeleft= i_to_do/rate; % seconds
disp([num2str(frac_now) '% completed of "' title '" loop. ' num2str(hours) ' hours, ' num2str(minutes) ' minutes and ' num2str(timeleft) ' seconds remaining at ' datestr(clock) '.'])