-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessIndicator.m
More file actions
170 lines (160 loc) · 5.68 KB
/
ProcessIndicator.m
File metadata and controls
170 lines (160 loc) · 5.68 KB
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
classdef ProcessIndicator < handle
% ProcessIndicator: A simple class that indicates process either via waitbar or text output
%
% The flag UseWaitbar determines if a waitbar is used or text output is produced. The text
% output restrains itself to only report progress at about each 10% to keep verbosity at an
% acceptable level.
%
% @author Daniel Wirtz @date 2012-03-20
%
% @change{0,6,dw,2012-05-07} The constructor takes varargin arguments that
% are forwarded to a sprintf of the title string, if given.
%
% @change{0,6,dw,2012-04-16} Fixed misinterpreted use of "step" method. Now
% have a step method which takes an increase value and a set method which
% takes the new absolute progress to show
%
% @new{0,6,dw,2012-03-20} Added this class.
%
% @todo process indicator for arbitrary (percentage/integer) values!
%
% This class is part of the framework
% KerMor - Model Order Reduction using Kernels:
% - \c Homepage http://www.morepas.org/software/index.html
% - \c Documentation http://www.morepas.org/software/kermor/index.html
% - \c License @ref licensing
properties
% Flag that indicates if a waitbar should be used.
%
% @default false @type logical
UseWaitbar = false;
end
properties(Access=private)
total;
p;
wb;
title;
cur;
end
methods
function this = ProcessIndicator(title, total, wb, varargin)
% Creates a new ProcessIndicator.
%
% If a total argument is given, the indicator is initialized via @code start(total)
% @endcode directly.
%
% Parameters:
% title: A title @type char @default 'Process running'
% total: The total process amount. Must be positive. @type double @default []
% wb: Flag that indicates how to initialize the UseWaitbar flag. @type logical
% @default false
% varargin: Any further parameters are passed to a sprintf call
% for the title string.
%
% Return values:
% this: The new ProcessIndicator @type ProcessIndicator
if nargin < 3
wb = false;
end
if ~isempty(varargin)
this.title = sprintf(title,varargin{:});
else
this.title = sprintf('%s (%d total)',title,total);
end
this.UseWaitbar = wb;
if nargin > 1
this.start(total);
end
end
function start(this, total)
% Starts the process indicator with the given total process amount.
%
% Parameters:
% total: The total process amount. Must be positive. @type double
if isempty(total) || ~isscalar(total) || total <= 0
error('Total must be a positve scalar value');
end
this.total = total;
this.cur = 0;
this.p = 0;
if this.UseWaitbar
this.wb = waitbar(0,this.title,'Visible','off');
% Place at main monitor middle
mpos = get(0,'MonitorPositions');
if size(mpos,1) > 1
npos = get(this.wb,'Position');
npos(1) = (mpos(1,3)/2-npos(3));
set(this.wb,'Position',npos);
end
set(this.wb,'Visible','on');
else
fprintf('%s: ',this.title);
end
end
function set(this, value)
% Directly sets the current process value.
%
% Value is always restricted to `[0, total]`, and if no argument is given, value=1
% is assumed.
if nargin == 1
error('Set requires a value to set.');
end
if value > this.total
value = this.total;
elseif value < 0
value = 0;
end
this.cur = value;
this.update;
end
function step(this, value)
% Reports process to the indicator and triggers waitbar or text output.
%
% Increases the current process by the given value. If no value
% is specified, one is assumed.
%
% Parameters:
% value: The process value. @type double @default 1
if nargin == 1
value = 1;
end
if value + this.cur > this.total
value = this.total - this.cur;
elseif value < -this.cur
value = -this.cur;
end
this.cur = this.cur + value;
this.update;
end
function stop(this)
% Stops the process indication and closes the waitbar, if any.
if this.UseWaitbar
if ~isempty(this.wb) && ishandle(this.wb)
close(this.wb);
end
else
fprintf('done!\n');
end
end
function set.UseWaitbar(this, value)
if ~islogical(value) || ~isscalar(value)
error('UseWaitbar must be a logical scalar');
end
this.UseWaitbar = value;
end
end
methods(Access=private)
function update(this)
perc = this.cur/this.total;
pstr = sprintf('%2.0f%% ',round(perc*100));
if this.UseWaitbar
waitbar(perc,this.wb,sprintf('%s: %s',this.title,pstr));
else
if perc > this.p
fprintf('%s',pstr);
this.p = ceil(perc*10)/10;
end
end
end
end
end