-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogress.m
85 lines (71 loc) · 1.58 KB
/
progress.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
% A progress bar for MATLAB in the terminal.
%
% Its usage follows a simple pattern:
% >> for i=1:n,
% >> some_operation(i);
% >> progress('some operation', i, n);
% >> end
%
% This will show a progress bar on the terminal, as well other statistics
% such as how long the operation has been running, or a prediction on how
% long it is going to take.
function progress(fid, msg, i, n, delayed),
if isstr(fid),
if exist('n', 'var'),
delayed = n;
end
n = i;
i = msg;
msg = fid;
fid = 1;
end
if ~exist('delayed', 'var'),
delayed = 0;
end
persistent t;
persistent tlast;
persistent tcount;
persistent tlasti;
if isempty(t) || i <= 1 || i < tlasti,
%uprintf(fid);
t = tic();
tcount = 0;
tlasti = 0;
end
if i < 0,
return;
end
if ~isempty(tlast) && toc(tlast) < 0.1 && i ~= n,
return;
end
if delayed > 0 && toc(t) < delayed,
return;
end
rate = i/toc(t);
[left, leftunits] = sec2human((n-i)/rate);
[total, totalunits] = sec2human(toc(t));
switch mod(tcount,4),
case 0, m = '|';
case 1, m = '/';
case 2, m = '-';
case 3, m = '\\';
end
if i == n,
m = '';
end
blen = 25;
b = ['|' repmat('=', 1, floor(i/n * blen)) m repmat(' ', 1, blen-floor(i/n*blen)-1) '|'];
tlast = tic();
tcount = tcount + 1;
parinfo = '';
numpars = getCurrentJob();
if ~isempty(numpars),
numpars = length(numpars.Tasks);
if numpars > 1,
parinfo = sprintf('%i labs, ', numpars);
end
end
uprintf(fid, '%s: %s %0.1f%%%% (%i of %i, %0.2f per sec, %s%0.2f %s taken, %0.2f %s left)', msg, b, i/n*100, i, n, rate, parinfo, total, totalunits, left, leftunits);
if i==n,
uprintf(fid);
end