-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathBF_ProgressBar.m
67 lines (60 loc) · 1.98 KB
/
BF_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
function BF_ProgressBar(progressProp,PB_length,theChar)
% Create/update a text progress bar.
%
%---INPUTS:
% progressProp: EITHER Text string to initialize ('new') or terminate ('close')
% OR Proportion progress (numerical)
% PB_length: Length of progress bar (in characters)
% theChar: Character to use for progress bar
%-------------------------------------------------------------------------------
%% Initialization
%-------------------------------------------------------------------------------
persistent progressBar; % Text progress bar
% Vizualization parameters
if nargin < 2
PB_length = 40; % Length of progress bar
end
if nargin < 3
theChar = ':'; % Character to show progress
end
notTheChar = ' ';
%-------------------------------------------------------------------------------
%% Main
%-------------------------------------------------------------------------------
if isempty(progressBar) && ischar(progressProp) && strcmp(progressProp,'new')
% Initialize progress bar
progressBar = -1;
elseif ~isempty(progressBar) && ischar(progressProp) && strcmp(progressProp,'close')
% Progress bar - termination
fprintf(1,'\n');
clear('progressBar')
elseif isnumeric(progressProp)
assert(progressProp >= 0)
assert(progressProp <= 1)
% Check whether update to progress bar is required
if round(progressProp*PB_length) > progressBar
% clear current text:
if progressBar~=-1
fprintf(repmat('\b',1,PB_length+2))
end
% Update and write:
progressBar = round(progressProp*PB_length);
WriteProgressBar
end
else
% Any other unexpected input
warning('Unexpected input ''%s''',progressProp);
progressBar = [];
end
function WriteProgressBar()
% Write progress bar text to commandline
fprintf('|');
for i = 1:progressBar
fprintf(theChar)
end
for i = 1:PB_length-progressBar
fprintf(1,notTheChar);
end
fprintf('|');
end
end