-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextprogressbar.m
More file actions
79 lines (71 loc) · 2.4 KB
/
textprogressbar.m
File metadata and controls
79 lines (71 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function textprogressbar(c)
% 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 prevc strCRtitle; % 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
%% Main
if nargin == 0
% Progress bar - force termination/initialization
fprintf('\n');
strCR = [];
strCRtitle = [];
prevc = [];
elseif ischar(c)
% Progress bar - set/reset title
if not(isempty(strCR)) && strCR(1) ~= -1
fprintf(strCR);
end
if not(isempty(strCRtitle))
fprintf(strCRtitle);
end
% add trailing space if not one already
if isempty(regexp(c,'\s$', 'once'))
c = [c ' '];
end
fprintf('%s',c);
strCR = -1;strCRtitle = repmat('\b',1,numel(c)+1);
elseif isnumeric(c)
% Progress bar - normal progress
if isempty(prevc)
prevc = 0;
end
c = floor(c);
if c == prevc
return
else
prevc = c;
end
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];
% 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);
if c == 100
fprintf('\n');
end
else
% Any other unexpected input
error('Unsupported argument type');
end