Filter löschen
Filter löschen

Adding start date to timestamps

3 Ansichten (letzte 30 Tage)
Oliver Higbee
Oliver Higbee am 5 Mär. 2019
Kommentiert: Oliver Higbee am 25 Apr. 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 Kommentar
Geoff Hayes
Geoff Hayes am 5 Mär. 2019
Oliver - you could try adding a reference datetime to your array. See date and time arithmetic for details.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Peter Perkins
Peter Perkins am 11 Mär. 2019
Bearbeitet: Peter Perkins am 12 Mär. 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
  1 Kommentar
Oliver Higbee
Oliver Higbee am 25 Apr. 2019
Thanks Peter, I did as you suggested and used readtable

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by