-
Notifications
You must be signed in to change notification settings - Fork 9
/
dispstat.m
83 lines (81 loc) · 2.8 KB
/
dispstat.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
function dispstat(TXT,varargin)
% Prints overwritable message to the command line. If you dont want to keep
% this message, call dispstat function with option 'keepthis'. If you want to
% keep the previous message, use option 'keepprev'. First argument must be
% the message.
% IMPORTANT! In the firt call, option 'init' must be used for initialization purposes.
% Options:
% 'init' this must be called in the begining. Otherwise, it can overwrite the previous outputs on the command line.
% 'keepthis' the message will be persistent, wont be overwritable,
% 'keepprev' the previous message wont be overwritten. New message will start from next line,
% 'timestamp' current time hh:mm:ss will be appended to the begining of the message.
% Example:
% clc;
% fprintf('12345677890\n');
% dispstat('','init') %Initialization. Does not print anything.
% dispstat('Time stamp will be written over this text.'); % First output
% dispstat('is current time.','timestamp','keepthis'); % Overwrites the previous output but this output wont be overwritten.
% dispstat(sprintf('*********\nDeveloped by %s\n*********','Kasim')); % does not overwrites the previous output
% dispstat('','timestamp','keepprev','keepthis'); % does not overwrites the previous output
% dispstat('this wont be overwriten','keepthis');
% dispstat('dummy dummy dummy');
% dispstat('final stat');
% % Output:
% 12345677890
% 15:15:34 is current time.
% *********
% Developed by Kasim
% *********
% 15:15:34
% this wont be overwriten
% final stat
% **********
% **** Options
keepthis = 0; % option for not overwriting
keepprev = 0;
timestamp = 0; % time stamp option
init = 0; % is it initialization step?
if ~ischar(TXT) %% changed from 'isstr()'
return
end
persistent prevCharCnt;
if isempty(prevCharCnt)
prevCharCnt = 0;
end
if nargin == 0
return
elseif nargin > 1
for i = 2:nargin
eval([varargin{i-1} '=1;']);
end
end
if init == 1
prevCharCnt = 0;
return;
end
if isempty(TXT) && timestamp == 0
return
end
if timestamp == 1
c = clock; % [year month day hour minute seconds]
txtTimeStamp = sprintf('%02d:%02d:%02d ',c(4),c(5),round(c(6)));
else
txtTimeStamp = '';
end
if keepprev == 1
prevCharCnt = 0;
end
% *************** Make safe for fprintf, replace control charachters
TXT = strrep(TXT,'%','%%');
TXT = strrep(TXT,'\','\\');
% *************** Print
TXT = [txtTimeStamp TXT '\n'];
fprintf([repmat('\b',1, prevCharCnt) TXT]); %% this is the main event
nof_extra = length(strfind(TXT,'%%'));
nof_extra = nof_extra + length(strfind(TXT,'\\'));
nof_extra = nof_extra + length(strfind(TXT,'\n'));
prevCharCnt = length(TXT) - nof_extra;
if keepthis == 1
prevCharCnt = 0;
end
end