prevent rounding in datevec
Show older comments
Hi there,
I'm currently trying to concatenate some time series data and I need to add the duration/time lapsed from the end of one block to the other. The time lapsed is written to a .txt like so '_00:17:27.450644'.
I have been trying to extract the hour, minute, seconds and milliseconds values to add these to the next trial which appears as '_00:00:00.049061'.
What I have been using is the following as this at least gives me three decimals of milliseconds
addtime = datestr(strrep(Timelapsed(2801), '_', ''), 'HH:MM:SS.FFF');
[~, ~, ~, H,MN,S] = datevec(strrep(Timelapsed(2801), '_', ''), 'HH:MM:SS.FFF')
But even after using format longE I get
H =
0
MN =
17
S =
2.745000000000000e+01
When really I need to get the exact value 27.450644.
I would like to be able to add these values (H,MN,S) to each value in the remaining trials, like so.
newtime = datestr(strrep(ROMOD22{2802, 5}, '_', ''), 'HH:MM:SS.FFF'); % for example '_00:00:00.049061'
[~, ~, ~, H1,MN1,S1] = datevec(newtime, 'HH:MM:SS.FFF');
newH = H+H1;
newMN = MN+MN1;
newS = S+S1;
str = ['_0' num2str(newH) ':' num2str(newMN) ':' num2str(newS)];
ROMOD22{2802, 5} = cellstr(str);
But then of course my result is rounded
'_00:17:27.499'
As really my result should be 00:17:27.499705. Although it doesn't seem like much I'm working with some extremely time sensitive data and need those extra milliseconds. I tried using datenum and datetime but datevec seems to be the only one that gives me milliseconds. If anyone has any idea on how to prevent the rounding I would very much appreciate it.
Kindly, Alie
Answers (1)
Walter Roberson
on 11 Oct 2016
This is simply not possible with datenum.
>> eps(now)*24*60*60
ans =
1.00582838058472e-05
This establishes that the time resolution is about 1E-5 seconds, which is about 10 microseconds, but you need time resolution down to 1 microsecond.
>> T = datetime(); T.Format = 'HH:mm:ss.SSSSSS'
T =
datetime
22:43:56.457037
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!