Unix-time-line, start with zero for plotting
12 views (last 30 days)
Show older comments
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')
0 Comments
Accepted Answer
Jan
on 10 Oct 2018
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')
More Answers (1)
Steven Lord
on 10 Oct 2018
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.
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!