Adding start date to timestamps
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Geoff Hayes
2019년 3월 5일
Oliver - you could try adding a reference datetime to your array. See date and time arithmetic for details.
채택된 답변
Peter Perkins
2019년 3월 11일
편집: Peter Perkins
2019년 3월 12일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!