Convert cell array datetime format to double array in HH:MM:SS format
Mostrar comentarios más antiguos
Hi,
I have a cell array for datetime format of 1441 x 1 as:
'2018-07-09 23:59:00.064834'
'2018-07-10 00:00:00.064833'
'2018-07-10 00:01:00.064829'
'2018-07-10 00:02:00.064832'
I want to express this array into 1441 x 1 double format but in the format of HH:MM:SS only
Can you help please?
Respuestas (1)
Steven Lord
el 23 de Jul. de 2020
Do you want the numbers as a double array (and if so, what does that double array represent? Seconds since a specific time? Seconds after midnight on the day the date and time information represents?) or do you want the numbers as a duration array?
% Raw text data
X = {'2018-07-09 23:59:00.064834'
'2018-07-10 00:00:00.064833'
'2018-07-10 00:01:00.064829'
'2018-07-10 00:02:00.064832'};
% Create a datetime array from the text
% Make it display in the same format as the text data
dt = datetime(X, 'Format', 'dd-MMM-uuuu HH:mm:ss.SSSSSS')
% Subtract midnight from the datetime array to give a duration
previousMidnight = dateshift(dt, 'start', 'day');
du = dt - previousMidnight;
% Tweak the format so you can see we haven't lost the fractional seconds
du.Format = 'hh:mm:ss.SSSSSS'
% du is in the right format (with the fractional seconds added) but it is not a double array
% Convert the duration into a number of seconds past midnight
s = seconds(du)
% s is not in the format you asked for but it is a double array
5 comentarios
Ahmed Darwish
el 23 de Jul. de 2020
Steven Lord
el 23 de Jul. de 2020
When you say "lookup table" do you mean you want to perform interpolation in MATLAB or do you mean you want to use it in one of the lookup table blocks in a Simulink model?
If you want to perform interpolation, interp1 can accept a duration array.
If you want to use it in a lookup table block, I don't believe they support duration arrays but neither colon nor multiple decimal points in one number (as shown in the second X array in your comment) are supported in a double array. [Well, colon can be used to create the array, but it can't be part of how the array is stored or displayed.]
You would also need to decide how you want to handle the gap between the first and second element of the second X array in your comment. For this you're probably going to want to convert to number of seconds / minutes / hours / etc. since some sort of epoch, but in doing so it's not going to display the way you've written it in your example.
Ahmed Darwish
el 23 de Jul. de 2020
Steven Lord
el 23 de Jul. de 2020
Can you give the name of the model (or post a link to the documentation page or example) that you're using as inspiration for what you're trying to do?
It sounds like you do want the number of seconds since an epoch. In this case I'll use the first (chronologically) date and time in X. [It happens for this data set that's also the first element in the array, but min will handle the case where the data set is out of order.]
% Raw text data
X = {'2018-07-09 23:59:00.064834'
'2018-07-10 00:00:00.064833'
'2018-07-10 00:01:00.064829'
'2018-07-10 00:02:00.064832'};
% Create a datetime array from the text
dt = datetime(X);
% Find the first/earliest date and time in the array
earliest = min(dt);
% Count the number of seconds since the earliest point in the array
s = seconds(dt-earliest)
Ahmed Darwish
el 23 de Jul. de 2020
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!