Reputation: 47
I'm trying to convert date in a string a vector of time, which as this format: 'HH:MM:SS', in a vector of time in seconds.It works well till a certain point and after it returns fake values.
Here is what I'm doing:
a={'596:30:18';'596:30:28';'596:30:38';'596:30:48';'596:30:58';'596:31:08';'596:31:18';'596:31:28';'596:31:38';'596:31:48';'596:31:58';'596:32:08';'596:32:18';'596:32:28';'596:32:38';'596:32:48';'596:32:58';'596:33:08'};
formatIn = 'HH:MM:SS';
DateVector = datevec(a(1:end,:),formatIn);
time = etime(DateVector,DateVector(1,:));
It returns:
DateVector =
2017 1 25 20 30 18
2017 1 25 20 30 28
2017 1 25 20 30 38
2017 1 25 20 30 48
2017 1 25 20 30 58
2017 1 25 20 31 8
2017 1 25 20 31 18
2016 12 7 3 28 40,7040000000000
2016 12 7 3 28 50,7040000000000
2016 12 7 3 29 0,704000000000000
2016 12 7 3 29 10,7040000000000
2016 12 7 3 29 20,7040000000000
2016 12 7 3 29 30,7040000000000
2016 12 7 3 29 40,7040000000000
2016 12 7 3 29 50,7040000000000
2016 12 7 3 30 0,704000000000000
2016 12 7 3 30 10,7040000000000
2016 12 7 3 30 20,7040000000000
and
time =
0
10
20
30
40
50
60
-4294897,29600000
-4294887,29600000
-4294877,29600000
-4294867,29600000
-4294857,29600000
-4294847,29600000
-4294837,29600000
-4294827,29600000
-4294817,29600000
-4294807,29600000
-4294797,29600000
only the 7 firt value are correct. Anybody knows why is this happening?
Upvotes: 1
Views: 1049
Reputation: 125854
You can do this pretty easily by first converting a
using datenum
and datetime
, subtracting the first time point, and converting to seconds:
D = datetime(datenum(a), 'ConvertFrom', 'datenum'); % A datetime array
secElapsed = seconds(D-D(1))
secElapsed =
0
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
Upvotes: 1
Reputation: 4510
here is another way without loops and way faster (in theory):
B = regexp(a, ':', 'split');
C = vertcat(B{:});
D = str2double(C); %D = cellfun(@str2num, C); also works but slower
this translates your time stamps to Hours:Minutes:Seconds in 3 columns, you can then quite easily subtract and get the time:
E = bsxfun(@minus,D,D(1,:));
F = E(:,1)*3600 + E(:,2)*60 + E(:,3)
>>F =
0
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
btw, what a strange datetime format.
Upvotes: 2
Reputation: 1196
The only explanation I can think of is that the HH
identifier is not intendet to be used with 3-digits hours. Actually the datevec documentation of datevec says
HH - Hour in two digits format
So maybe, '596:30:28'
is not a valid date string and you need to convert it to a different format, e.g. 'dd:HH:MM:SS'
.
This can be done like this (assuming that all hours are 3-digit numbers):
a={'596:30:18';'596:30:28';'596:30:38';'596:30:48';'596:30:58';'596:31:08';'596:31:18';'596:31:28';'596:31:38';'596:31:48';'596:31:58';'596:32:08';'596:32:18';'596:32:28';'596:32:38';'596:32:48';'596:32:58';'596:33:08'};
a_ddhhmmss = {};
for ii=1:length(a)
datestring = a{ii};
colon_indices = strfind(datestring, ':'); %check how many digits the hours have
hours_digits = colon_indices(1)-1;
hours = str2num(datestring(1:hours_digits));
days = floor(hours/24) + 1; % start with day=1
hours = rem(hours,24);
a_ddhhmmss{end+1} = [num2str(days), ':', num2str(hours), datestring(4:end)];
end
formatIn = 'dd:HH:MM:SS';
DateVector = datevec(a_ddhhmmss(1:end,:),formatIn)
time = etime(DateVector,DateVector(1,:))
Of course, it still makes no sense that the behavior you observe just sets in between the dates '596:30:18'
and '596:30:28'
. Even if the datevec
function is used out of scope, it should rather throw an error instead of returning meaningless output.
Upvotes: 1