-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopupProgress.m
246 lines (204 loc) · 6.79 KB
/
PopupProgress.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
function PopupProgress(name, totalTicks, varargin)
% PopupProgress(name, totalTicks, updatePeriod, keepUnderscores)
% Create or update a progress indicator.
% Note: in a parallel environment, uses a temporary file
% -To create the progress indicator, pass the name, the total number of
% ticks (updates to be completed) and if desired, specify the period to
% update estimated completion time.
% -To increment progress, pass only the name of an existing indicator.
% The indicator automatically closes if progress is complete.
% INPUTS:
% name: string, describing the progress indicator
% OPTIONAL:
% totalTicks: the total number of progress updates until completion
% updatePeriod: interval (in seconds) between updated estimates of
% completion time. Defaults to 5s.
% keepUnderscores: interpret underscores as characters, not subscripts.
% Defaults to true
if nargin == 1
% increment progress bar
incrementPopupTicks(name);
else
% get options
defaultOptions = { ...
'updatePeriod', 5, ...
'keepUnderscores', true, ...
'errorOnPreexisting', true ...
};
options = GetOptions(defaultOptions, varargin);
makePopupTicks(name, totalTicks, options)
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function makePopupTicks(name, totalTicks, options)
% Create a progress bar that can be updated with future calls
% 1. Progress (number of ticks) is stored in a temporary file.
% 2. A figure is created with
% a) a waitbar
% b) a timer
% c) a callback function actived by the timer, which monitors the
% temporary file and updates the waitbar appropriately
invalidChars = unique(name(regexp(name, '[^ .a-zA-Z_0-9\-]')));
if any(invalidChars)
error('PopupProgress name ''%s'' contains invalid character(s): %s', ...
name, invalidChars)
end
if any(findobj('Name', name))
% There is already a progress bar with this name
% delete the temporary file holding the tick count
delete([tempdir, name, '_matlabTimer.txt'])
if options.errorOnPreexisting
error('There is already a progress bar named ''%s''', name)
else
% just close the old progress bar and continue
close(findobj('Name', name));
end
end
% store the tick count in a temporary file
if options.keepUnderscores
update.name = realUnderscores(name);
else
update.name = name;
end
update.fileName = [tempdir, name, '_matlabTimer.txt'];
fid = fopen(update.fileName, 'w');
if fid < 0
error('Couldn''t create temporary file: %s', update.fileName)
end
% close the file, leaving it empty
fclose(fid);
% create the waitbar figure
tickStr = sprintf('%lu / %lu', 0, totalTicks);
message = {update.name, tickStr, ''};
update.totalTicks = totalTicks;
update.updatePeriod = options.updatePeriod;
update.t0 = tic;
h = waitbar(0, message, 'Name', name);
set(h, 'HandleVisibility', 'on')
hAxes = get(h, 'CurrentAxes');
set(hAxes, 'FontName', 'Arial', 'FontSize', 14)
update.waitbarHandle = h;
% create the timer and set the callback functions
% the callback function checks the temporary file and updates the waitbar
% appropriately. Once the last tick is called, everything closes
% update structure is stored in the timer's UserData
t = timer('tag', 'progresstimer', ...
'name', name, ...
'timerfcn', @popupCallback, ...
'stopfcn', @popupStopCallback, ...
'period', update.updatePeriod, ...
'StartDelay', update.updatePeriod, ...
'ExecutionMode', 'FixedDelay', ...
'UserData', update);
% start the timer
start(t);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function incrementPopupTicks(name)
% Increment the ticks in the progress bar
% Increment ticks using a temporary file
% This method works even in a parallel environment (e.g. with parfor)
fileName = [tempdir, name, '_matlabTimer.txt'];
fid = fopen(fileName, 'a');
if fid < 0
error('couldn''t append to file %s', fileName)
end
fwrite(fid, ' ', 'char*1');
fclose(fid);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function popupCallback(obj, event, string_arg) %#ok<INUSD>
% monitor the progress via the temporary file, and update the progress bar
% appropriately
% get the update structure from the timer's (obj) UserData
update = get(obj, 'UserData');
% get the number of ticks, as the length of the file
fid = fopen(update.fileName, 'r');
if fid < 0
error('Couldn''t open file %s', update.fileName)
end
fseek(fid, 0, 'eof');
ticks = ftell(fid);
fclose(fid);
if ticks == 0
% no progress, wait for next check
return
end
% update the tickStr
tickStr = ...
sprintf('%lu / %lu', ticks, update.totalTicks);
% calculate the length of the bar on the waitbar (the fraction complete)
frac = ticks / update.totalTicks;
% check the total elapsed time
tNow = toc(update.t0);
% estimate the remaining time
tFinish = (update.totalTicks - ticks ) * tNow / ticks;
timeRemaining = [getTimeString(tFinish), ' remaining'];
message = {update.name, tickStr, timeRemaining};
% update the waitbar
if ishandle(update.waitbarHandle)
% check to make sure the waitbar still exists, preventing a warning if
% the progress bar is deleted by an external routine
waitbar(frac, update.waitbarHandle, message);
end
if ticks >= update.totalTicks
% progress is complete, stop the timer
stop(obj)
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function popupStopCallback(obj, event, string_arg) %#ok<INUSD>
% shutdown the progress bar
% get the update structure
update = get(obj, 'UserData');
% delete the timer
delete(obj)
% close the waitbar
if ishandle(update.waitbarHandle)
close(update.waitbarHandle);
end
% delete the temporary file
if exist(update.fileName, 'file')
delete(update.fileName);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function timeString = getTimeString(tFinish)
% convert a time in seconds into a human-friendly string
s = round(tFinish);
if s < 60
timeString = sprintf('%ds', s);
return
end
m = floor(s/60);
s = s - m * 60;
if m < 60
timeString = sprintf('%dm%ds', m, s);
return
end
h = floor(m/60);
m = m - h * 60;
if h < 24
timeString = sprintf('%dh%dm%ds', h, m, s);
return
end
d = floor(h/24);
h = h - d * 24;
timeString = sprintf('%gd%dh%dm%ds', d, h, m, s);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function texSafeString = realUnderscores(titleStr)
% Protects the underscores in titleStr from being interpreted as
% subscript commands by inserting a '\' before them.
ind = strfind(titleStr, '_');
texSafeString = '';
previous = 1;
for n = 1:length(ind)
if ind(n) > 1
texSafeString = [texSafeString, titleStr(previous:(ind(n)-1))]; %#ok<*AGROW>
end
texSafeString = [texSafeString, '\'];
previous = ind(n);
end
texSafeString = [texSafeString, titleStr(previous:end)];
return