Adding start date to timestamps

1 回表示 (過去 30 日間)
Oliver Higbee
Oliver Higbee 2019 年 3 月 5 日
コメント済み: Oliver Higbee 2019 年 4 月 25 日
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
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
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
  1 件のコメント
Oliver Higbee
Oliver Higbee 2019 年 4 月 25 日
Thanks Peter, I did as you suggested and used readtable

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by