How to properly display microsecond time differences

I have a timetable with a time column containing microsecond resolution. The time column contains something like this:
'2020-11-07 21:58:29.468130'
'2020-11-07 21:58:29.482676'
'2020-11-07 21:58:29.517115'
'2020-11-07 21:58:29.655223'
'2020-11-07 21:58:29.779059'
'2020-11-07 21:58:29.918082'
'2020-11-07 21:58:29.942628'
This is correctly displayed in the data browser. However, when I run a diff on the column the resulting vector just shows 00:00:00 through-out. When I plot the vector, on the other hand, it properly plots the microsecond differences. So it contains the necessary resolution, but it does not properly display it. This goes also for any other function result (for instance using mean() on the vector, as long as the result is below the second resolution. Is there a way to ensure that all such time data is displays globally with the proper resolution?

 Accepted Answer

Change the Format for the datetime and duration arrays you create.
t = {'2020-11-07 21:58:29.468130'
'2020-11-07 21:58:29.482676'
'2020-11-07 21:58:29.517115'
'2020-11-07 21:58:29.655223'
'2020-11-07 21:58:29.779059'
'2020-11-07 21:58:29.918082'
'2020-11-07 21:58:29.942628'}
t = 7x1 cell array
{'2020-11-07 21:58:29.468130'} {'2020-11-07 21:58:29.482676'} {'2020-11-07 21:58:29.517115'} {'2020-11-07 21:58:29.655223'} {'2020-11-07 21:58:29.779059'} {'2020-11-07 21:58:29.918082'} {'2020-11-07 21:58:29.942628'}
Convert the text forms of the dates and times into a datetime array.
dt = datetime(t)
dt = 7×1 datetime array
07-Nov-2020 21:58:29 07-Nov-2020 21:58:29 07-Nov-2020 21:58:29 07-Nov-2020 21:58:29 07-Nov-2020 21:58:29 07-Nov-2020 21:58:29 07-Nov-2020 21:58:29
By default, the Format property of the datetime does not include fractional seconds. Let's modify the Format so it does.
dt.Format = dt.Format + ".SSSSSS"
dt = 7×1 datetime array
07-Nov-2020 21:58:29.468130 07-Nov-2020 21:58:29.482676 07-Nov-2020 21:58:29.517115 07-Nov-2020 21:58:29.655223 07-Nov-2020 21:58:29.779059 07-Nov-2020 21:58:29.918082 07-Nov-2020 21:58:29.942628
Take the difference between adjacent elements of dt.
delta = diff(dt)
delta = 6×1 duration array
00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00
As with dt, the default Format for delta does not include fractional seconds. But we can fix that.
delta.Format = delta.Format + ".SSSSSS"
delta = 6×1 duration array
00:00:00.014545 00:00:00.034439 00:00:00.138107 00:00:00.123836 00:00:00.139023 00:00:00.024546

1 Comment

I see. Does that mean that for every variable that requires microsecond resolution, I need to separately define the format, for it to be displayed correctly? Is there no way to set this globally?

Sign in to comment.

More Answers (0)

Categories

Asked:

on 12 Nov 2020

Commented:

on 12 Nov 2020

Community Treasure Hunt

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

Start Hunting!