Adding start date to timestamps
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Oliver Higbee
el 5 de Mzo. de 2019
Comentada: Oliver Higbee
el 25 de Abr. de 2019
I'm importing data from a .csv file containing temperatures logged every 30 seconds for 4 days. There is a column containg timestamps but with no date information.
I would like to import this in matlab and add dates to the timestamps, I'm using 'datetime' to get it into the format I would like using;
conductor_temps=xlsread('1March19to5March19_data_set2.csv');
TimeStamps = datetime(conductor_temps(:,2), 'ConvertFrom', 'excel');
This assigns 31-Dec-1899 to each timestamp due to the lack of date information. I can use;
TimeStamps.Day = 1;
TimeStamps.Month = 3;
TimeStamps.Year = 2019;
And manually assign. Is there a more elegant way to do this. For example assign a start date to the dataset and have it move to the following day each time the timestamps reaches midnight?
1 comentario
Geoff Hayes
el 5 de Mzo. de 2019
Oliver - you could try adding a reference datetime to your array. See date and time arithmetic for details.
Respuesta aceptada
Peter Perkins
el 11 de Mzo. de 2019
Editada: Peter Perkins
el 12 de Mzo. de 2019
Oliver, it's not 100% clear what you have, but I think this will do what you want. The main issue is, I guess, that your spreadsheet is formatted kind of funny, with no date info at all. I'm showing this done in raw workspace variables, but I actually recommend that you use readtable and work in the table you get back.
>> timestamps = repmat([0 43200]'/86400,3,1) % what I think you have
timestamps =
0
0.5000
0
0.5000
0
0.5000
>> t = datetime(timestamps,'ConvertFrom','Excel')
t =
6×1 datetime array
31-Dec-1899 00:00:00
31-Dec-1899 12:00:00
31-Dec-1899 00:00:00
31-Dec-1899 12:00:00
31-Dec-1899 00:00:00
31-Dec-1899 12:00:00
>> newday = [true; diff(t)<0]
newday =
6×1 logical array
1
0
1
0
1
0
>> daynum = cumsum(newday)
daynum =
1
1
2
2
3
3
>> date = datetime(2019,3,1) + caldays(daynum-1)
date =
6×1 datetime array
01-Mar-2019
01-Mar-2019
02-Mar-2019
02-Mar-2019
03-Mar-2019
03-Mar-2019
>> time = t - datetime(1899,12,31) % Excel serial day number epoch
time =
6×1 duration array
00:00:00
12:00:00
00:00:00
12:00:00
00:00:00
12:00:00
>> dt = date + time
dt =
6×1 datetime array
01-Mar-2019 00:00:00
01-Mar-2019 12:00:00
02-Mar-2019 00:00:00
02-Mar-2019 12:00:00
03-Mar-2019 00:00:00
03-Mar-2019 12:00:00
Más respuestas (0)
Ver también
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!