-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadsec.m
50 lines (40 loc) · 1.09 KB
/
readsec.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
function varargout = readsec(t)
%READSEC(T) converts seconds to a readable format (DAY:HH:MM:SS.mmm)
% It is handy when evaluating tic/toc output, e.g.:
% tic; %do stuff; t = toc;
% readsec(t);
%__________________________________________________________________________
%Daniele Mascali - [email protected]
if nargin == 0
help readsec
return
end
days = 0;
hours = 0;
mins = 0;
time_string = [];
if t >= 24*3600
days = floor(t/(24*3600));
time_string = [time_string,num2str(days),' day',plural(days),', '];
end
if t >= 3600
hours = floor((t-days*(24*3600))/3600);
time_string = [time_string,num2str(hours),' hour',plural(hours),', '];
end
if t >= 60
mins = floor((t-days*(24*3600)-hours*(3600))/60);
time_string = [time_string,num2str(mins),' min and '];
end
sec = t-days*(24*3600)-hours*(3600)-mins*60;
time_string = [time_string,sprintf('%2.3f s',sec)];
if nargout == 0
disp([num2str(t,'%15.3f'),'s -> ',time_string]);
else
varargout{1} = time_string;
end
return
end
function str = plural(n)
if n == 1; str = ''; else; str = 's'; end
return
end