Skip to content

Instantly share code, notes, and snippets.

@dineshj1
Last active January 27, 2017 20:53
Show Gist options
  • Save dineshj1/7d620818e954c9114daab3e109764f5c to your computer and use it in GitHub Desktop.
Save dineshj1/7d620818e954c9114daab3e109764f5c to your computer and use it in GitHub Desktop.
Textprogressbar from Matlab File Exchange with small mods
%demo_textprogressbar
%This a demo for textprogressbar script
textprogressbar('calculating outputs: ');
for i=1:100,
textprogressbar(i);
pause(0.1);
end
textprogressbar('done');
textprogressbar('saving data: ');
for i=1:0.5:80,
textprogressbar(i);
pause(0.05);
end
textprogressbar('terminated');
%This a demo for textprogressbar script
textprogressbar('saving data: ');
t=tic;
for i=1:0.5:80,
sfx_str=sprintf('Elapsed: %.2fs, ETA: %.2fs', toc(t), toc(t)/(i+eps)*(80-i));
textprogressbar(i/80*100, sfx_str);
pause(0.05);
end
textprogressbar('terminated');
function textprogressbar(c, sfx_str)
% This function creates a text progress bar. It should be called with a
% STRING argument to initialize and terminate. Otherwise the number correspoding
% to progress in % should be supplied.
% INPUTS: C Either: Text string to initialize or terminate
% Percentage number to show progress
% OUTPUTS: N/A
% Example: Please refer to demo_textprogressbar.m
% Author: Paul Proteus (e-mail: proteus.paul (at) yahoo (dot) com)
% Version: 1.0
% Changes tracker: 29.06.2010 - First version
% Inspired by: http://blogs.mathworks.com/loren/2007/08/01/monitoring-progress-of-a-calculation/
%% Initialization
persistent strCR; % Carriage return pesistent variable
% Vizualization parameters
strPercentageLength = 10; % Length of percentage string (must be >5)
strDotsMaximum = 10; % The total number of dots in a progress bar
if nargin<2
sfx_str='';
end
%% Main
if isempty(strCR) && ~ischar(c),
% Progress bar must be initialized with a string
error('The text progress must be initialized with a string');
elseif isempty(strCR) && ischar(c),
% Progress bar - initialization
fprintf('%s',c);
strCR = -1;
elseif ~isempty(strCR) && ischar(c),
% Progress bar - termination
strCR = [];
fprintf([c '\n']);
elseif isnumeric(c)
% Progress bar - normal progress
c = floor(c);
percentageOut = [num2str(c) '%%'];
percentageOut = [percentageOut repmat(' ',1,strPercentageLength-length(percentageOut)-1)];
nDots = floor(c/100*strDotsMaximum);
dotOut = ['[' repmat('.',1,nDots) repmat(' ',1,strDotsMaximum-nDots) ']'];
strOut = [percentageOut dotOut sfx_str];
% Print it on the screen
if strCR == -1,
% Don't do carriage return during first run
fprintf(strOut);
else
% Do it during all the other runs
fprintf([strCR strOut]);
end
% Update carriage return
strCR = repmat('\b',1,length(strOut)-1);
else
% Any other unexpected input
error('Unsupported argument type');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment