Convert cell array datetime format to double array in HH:MM:SS format

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)

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

Actually what I want to do,
I want to express the output as a numbers not in hours format like
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'};
to be :
X = [23.59:00.064834
00.00.00.064833
00.01.00.064829
00.02.00.064832
]
so I can use this time data in lookup table because it accept only a double structure array not a cell array
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.
I wanted to be in Simulink lookuptable, however one of the Matlab Simulink ready models have already did it with only decimel point for the time and it work fine in Matlab Simulink lookup table, so that I wanted to try the same but I could not actually,
This readings is for Solar irradiance with sampling one minute so that it is over 24 hours and I wanted to implement lookup table between the irradiance and the time but, the time expressed in this cell array and so that lookup table dont accept it
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)
the model name is power_V2G , just type that on matlab command and you will see the model, inside the irradirance block, you will find the lookup table and you will see how he already make the time as double array for 24 hour day with only decimal number

Iniciar sesión para comentar.

Categorías

Preguntada:

el 23 de Jul. de 2020

Comentada:

el 23 de Jul. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by