Unix-time-line, start with zero for plotting

9 ビュー (過去 30 日間)
Natasha
Natasha 2018 年 10 月 10 日
コメント済み: Natasha 2018 年 10 月 11 日
Hi,
I have a data recorded together with unix-time (milliseconds resolution). I need to convert the time to a human readable format and plot my data with corresponding time, which has to start with 0. Timestamp is not constant. For now I met a problem to make timeline start with zero. Does anybody know how I can solve this problem?
Here is how I converted time:
unixtime = [1.537602860041000e+12; 1.537602860561000e+12; 1.537602861081000e+12]
time = datetime(unixtime/1000,'ConvertFrom','posixTime','Format','mm.ss.SSS')

採用された回答

Jan
Jan 2018 年 10 月 10 日
Do you mean:
unixtime = [1.537602860041000e+12; 1.537602860561000e+12; 1.537602861081000e+12]
utime0 = unixtime - unixtime(1);
time = datetime(utime0 / 1000,'ConvertFrom','posixTime','Format','mm.ss.SSS')
  1 件のコメント
Natasha
Natasha 2018 年 10 月 11 日
This is exactly what I was looking for! Thanks a lot!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2018 年 10 月 10 日
What do you consider zero for this application? 00:00:00 UTC on Thursday, 1 January 1970? 00.00.000 on the date on which all of your dates fall? The start of the minute in which your data falls? Something else?
For the first, convert 0 into a datetime using 'ConvertFrom', 'posixTime'. I appended it to the unixtime vector, though you could convert it separately if you don't want to modify that vector. For all these examples, I'm hard-coding the information that the third element of your time vector is the latest time. You could use max if you don't want to make that assumption about your actual data.
>> unixtime(end+1) = 0;
>> time = datetime(unixtime/1000,'ConvertFrom','posixTime','Format','mm.ss.SSS')
>> plot(time(1:3), 1:3)
>> xlim(time([4 3]))
Note that all of your data is in the line that coincides with the right side of the axes, since the first and last of your data points differ by about a second and a half of time and the axes spans 48 years.
For the second, using the start of the day as 0:
>> plot(time(1:3), 1:3)
>> startOfDay = dateshift(time(1), 'start', 'day');
>> xlim([startOfDay, time(3)])
Again, your data is squished along the right side. This time the axes only spans about 8 hours, but your data still only covers a second and a half of that time.
Starting with the start of the minute containing the data is similar to the second approach:
>> plot(time(1:3), 1:3)
>> startOfDay = dateshift(time(1), 'start', 'minute');
>> xlim([startOfDay, time(3)])
Here you can actually see the curve, since the whole axes now spans about 20 seconds.
  1 件のコメント
Natasha
Natasha 2018 年 10 月 11 日
I meant that only time stamps are important for me but not absolute time itself. I found an answer (I just used unixtime - unixtime(1)) and I learned a lot from you! Thanks!

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by