-
Notifications
You must be signed in to change notification settings - Fork 0
/
phzUtil_progressbar.m
86 lines (73 loc) · 2.55 KB
/
phzUtil_progressbar.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
%PHZUTIL_PROGRESSBAR Text progress bar in the command window.
%
% Usage:
% w = phzUtil_progressbar(w,val,str)
%
% Input:
% w = [string] Pass an empty value ('') to initiate the progress bar.
% This function will return a value for w (the number of
% characters to delete on the next loop), so pass that value in
% here on the next loop.
%
% val = [numeric] Value between 0 and 1 indicating how much progress
% is complete. When a value of 1 is passed, the progress bar is
% printed with a newline ('\n') at the end.
%
% str = [string] Optional text to display on the line above the
% progress bar.
%
% Output:
% w = The string that was printed for the current update of the
% progress bar. Pass this variable in on the next loop so that
% the function knows how many characters to delete before
% printing the next progress update.
%
% Copyright (C) 2018 Gabriel A. Nespoli, gabenespoli@gmail.com
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see http://www.gnu.org/licenses/.
function w = phzUtil_progressbar(w,val,str)
waitchar = '=';
if nargin < 3
str = '';
extraChars = 1;
else
str = [str '\n'];
extraChars = 2;
end
if usejava('desktop') % using matlab gui
del = repmat('\b',1,length(w)-1-extraChars); % extra -2 for the newline chars
dot = floor(val * 20);
pct = num2str(floor(val * 100));
pct = [repmat(' ',1,3-length(pct)),pct,'%% '];
w = ['[',repmat(waitchar,1,dot),repmat(' ',1,20 - dot) '] ',pct];
w = [str w '\n'];
fprintf([del w])
else % using -nodisplay option to run matlab in a terminal
if isempty(w)
fprintf(' %s\n', str(1:end-2)) % \n has already been appended
fprintf(' [%s]\n', repmat(' ', 1, 20))
fprintf(' [')
w = 0;
return
end
dot = floor(val * 20);
if dot > w
fprintf('%s', repmat(waitchar, 1, dot - w))
w = dot;
end
if val == 1
fprintf('] Done.\n')
end
end
end