how to combine date and time

3 visualizaciones (últimos 30 días)
Boss Man
Boss Man el 22 de Feb. de 2020
Respondida: Star Strider el 22 de Feb. de 2020
So i have my data file attached
want to combine the data and time
then using the combined datetime produce different temptime graphs of diff days

Respuesta aceptada

Star Strider
Star Strider el 22 de Feb. de 2020
In theory at least, datetime should be able to do this seamlessly using the timeofday function for the seconds. In practice, it cannot do it at all, at least when I tried it, so I resorted to datenum to do the necessary arithmetic to combine the dates and times.
Try this:
D = readtable('data.txt','ReadVariableNames',1);
DNS = datenum([zeros(size(D,1),5) D{:,2}]);
DND = datenum(compose('%s',D{:,1}),'dd/mm/yyyy');
DateTime = datetime(DND + DNS, 'ConvertFrom','datenum');
When I looked at the results with:
Check = DateTime([1:20:1000]);
it seemed to be converting them correctly. I may have gotten the days and months reversed, since the date format is not obvious. Correct that if necessary by changing ‘'dd/mm/yyyy'’ to ‘'mm/dd/yyyy'’.
The compose call takes a while, so in the interests of speed, save ‘DateTime’ to a .mat file to load quickly when you want to use it later.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by