-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressText.m
executable file
·147 lines (123 loc) · 4.31 KB
/
progressText.m
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function progressText(fractionDone,text)
%PROGRESSTEXT shows progress of a loop as text on the screen
%
% SYNOPSIS: progressText(fractionDone,text)
%
% INPUT fractionDone: fraction of loop done (0-1)
% text (opt): {yourTexthere} : XX% done xx:xx:xx remaining
% Note: text can be changed for every call
% OUTPUT none
%
% EXAMPLE
% n = 1000;
% progressText(0,'Test run') % Create text
% for i = 1:n
% pause(0.01) % Do something important
% progressText(i/n) % Update text
% end
%
% REMARKS progressText will set lastwarn to ''
%
% created with MATLAB ver.: 7.4.0.287 (R2007a) on Windows_NT
%
% created by: jdorn based on progressbar.m
% DATE: 29-Jun-2007
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent starttime lastupdate clearText printText finalText warnText clearWarn
% constants
nCharsBase = 27; % change this if changing output format
% Test input
if nargin < 1 || isempty(fractionDone)
fractionDone = 0;
end
if nargin < 2 || isempty(text)
text = '';
else
text = [text,' : '];
end
if fractionDone == 0 || isempty(starttime)
% set up everything
% Set time of last update to ensure calculation
lastupdate = clock - 1;
% Task starting time reference
starttime = clock;
% create fprintf-expression
printText = sprintf('%s%%2d%%%% done %%s remaining',text);
initialText = sprintf('%s 0%%%% done xx:xx:xx remaining',text);
finalText = sprintf('%s100%%%% done %%s elapsed',text);
% get length of fprintf expression
nChars = nCharsBase + length(text);
clearText = repmat('\b',1,nChars);
% print initialText and return
fprintf(1,initialText);
% %fprintfExpression removes old expression before overwriting
% fprintfExpression = [clearText printText];
% fprintfExpressionFinal = [clearText, finalText];
% empty warning
lastwarn('');
warnText = '';
clearWarn = '';
return
elseif ~isempty(text)
% text has been changed. Create fprintfExpressions first, then update
% clearText
printText = sprintf('%s%%2d%%%% done %%s remaining',text);
finalText = sprintf('%s100%%%% done %%s elapsed',text);
fprintfExpression = [clearText clearWarn printText, warnText];
fprintfExpressionFinal = [clearText, clearWarn, finalText, warnText, '\n'];
nChars = nCharsBase + length(text);
clearText = repmat('\b',1,nChars);
elseif ~isempty(lastwarn)
% add warnings to the end of the progressText
% find warning
w = lastwarn;
lastwarn('')
nw = length(w);
% erase warning
fprintf(1,repmat('\b',1,11+nw)); %--- is this correct???
% create new warnText
w = regexprep(w,['(',char(10),'\s+)'],' - ');
warnTextNew = sprintf('\n Warning @%7.3f%%%% done : %s ',100*fractionDone,w);
warnLength = length(warnTextNew) - 1; % subtract 2 for %%-sign
warnText = [warnText,warnTextNew];
fprintfExpression = [clearText clearWarn printText, warnText];
fprintfExpressionFinal = [clearText, clearWarn, finalText, warnText, '\n'];
% prepare cleanWarn for next time
clearWarn = [clearWarn,repmat('\b',1,warnLength)];
else
% all is normal. Just generate output
fprintfExpression = [clearText clearWarn printText, warnText];
fprintfExpressionFinal = [clearText, clearWarn, finalText, warnText, '\n'];
end
% write progress
percentDone = floor(100*fractionDone);
% get elapsed time
runTime = etime(clock,starttime);
% check whether there has been a warning since last time
% if ~isempty(lastwarn)
% lastwarn('');
% fprintfExpression = regexprep(fprintfExpression,'(\\b)*','\\n');
% fprintfExpressionFinal = regexprep(fprintfExpressionFinal,'(\\b)*','\\b\\n');
% end
if percentDone == 100 % Task completed
fprintf(1,fprintfExpressionFinal,convertTime(runTime)); % finish up
clear starttime lastupdate clearText printText finalText % Clear persistent vars
return
end
% only update if significant time has passed
if etime(clock,lastupdate) < 0.3
return
end
% update
timeLeft = runTime/fractionDone - runTime;
fprintf(1,fprintfExpression,percentDone,convertTime(timeLeft));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subfcn
function timeString = convertTime(time)
timeStruct = sec2struct(time);
if timeStruct.hour > 99
timeString = '99:59:59';
else
timeString = timeStruct.str(1:end-4);
end