File Exchange

image thumbnail

Percent completion in a loop

version 1.0 (1.53 KB) by

Display the percentage of loop done.



View License

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.

Comments and Ratings (2)

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:


function prcdone(iloop,itot,title,dprc,tstart)
%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
% Example:
% time_of_start=tic;
% for i0 = 1:100
% pause(0.15)
% prcdone(i0,100,'test_loop',10,time_of_start)
% end

% Original Author: Arnaud Laurent
% Creation : Dec 4th 2012
% Updates: Matthew Gruber
% Nov 1st 2017
% MATLAB version: R2015a
% Last modified: November 11th 2017

if nargin<3
title = ' unnamed ';

if nargin<4

frac_now = dprc*(ceil(iloop/itot*100/dprc));
frac_next = dprc*(ceil((iloop+1)/itot*100/dprc));

if iloop == 1
disp(['Starting ' title ' loop'])
elseif iloop==itot
disp(['Done ' title ' loop'])
elseif frac_next>frac_now

if nargin<5
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) '.'])


MATLAB Release
MATLAB 8.0 (R2012b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today