Convert UNIX NanoSeconds to DTG in Milliseconds

8 views (last 30 days)
Timothy
Timothy on 18 Aug 2023
Answered: dpb on 19 Aug 2023
Hello,
I have to do 2 things: convert UNIX nanoseconds to DTG Nanoseconds, and then convert DTG Nanoseconds to DTG Milliseconds. The second conversion would be a "SUM" of all the entries into one millisecond. So for every nanosecond entry within millisecond "1," I would like to sum the different parts of the table and add them together to create a millisecond register instead of a nanosecond register. Does this make sense? Any questions are welcomed in order to reach this goal!
Thanks,
Tim
  8 Comments
Timothy
Timothy on 19 Aug 2023
Now that I have it in Date-Time with nanoseconds, any ideas how to simply round it off to milliseconds?
Walter Roberson
Walter Roberson on 19 Aug 2023
If you had the time in int64 or uint64 nanoseconds, then to round to milliseconds, just divide them by 1000: operations on integer data classes automatically round
If you have double precision numbers, then see round which has the ability to indicate how many digits you want to round to.

Sign in to comment.

Answers (1)

dpb
dpb on 19 Aug 2023
Ah! That simplifies the problem immensely; you've already passed the hard part.
To do the initial request to sum over millisec time intervals, turn the data table into a timetable and use retime with the 'regular' sample rate and @sum method...
tt=timetable(yourdatetimevar,restofdata);
ttMS=retime(tt,'regular','sum','SampleRate',1000); % aggregate a msec sample rate
The above will start at the actual time in msec and aggreate within those groups; it will not resample to approximate at even msec times. If want that, use retime() and interpolate first, then aggregate. There may be a way to do both in one step, I'm not sure.

Categories

Find more on Data Type Conversion 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!