How do I get the value of seconds in a datetime table?

16 views (last 30 days)
I collected some acceleration values using the matlab application, however i don't know how to convert the timestamps to seconds elapased.
  2 Comments
dpb
dpb on 3 Dec 2020
'Pends. Are the timestamps real time or some other measure?
If real time, then just subtract the first value from the data.
Hessa Lootah
Hessa Lootah on 3 Dec 2020
the timestamps from the sensor came out with the date(dd:mm:yy), time in hours, min, second and millisecond.
so basically subtract the first value from the last value and figure out how long that took?

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 3 Dec 2020
Edited: Cris LaPierre on 3 Dec 2020
+1 to dpb's comment. Subtracting datetimes gives you a duration. You can convert your duration to your desired units - days, hours, minutes, seconds, etc by setting the duration format. See more here.
Some code taken from this example.
% Create a vetor of times
t = datetime('now') + minutes(1:0.25:3)
t = 1×9 datetime array
03-Dec-2020 23:09:58 03-Dec-2020 23:10:13 03-Dec-2020 23:10:28 03-Dec-2020 23:10:43 03-Dec-2020 23:10:58 03-Dec-2020 23:11:13 03-Dec-2020 23:11:28 03-Dec-2020 23:11:43 03-Dec-2020 23:11:58
% compute elapsed time
dt = t - t(1)
dt = 1×9 duration array
00:00:00 00:00:15 00:00:30 00:00:45 00:01:00 00:01:15 00:01:30 00:01:45 00:02:00
% convert elapsed time to seconds
dt.Format = 's'
dt = 1×9 duration array
0 sec 15 sec 30 sec 45 sec 60 sec 75 sec 90 sec 105 sec 120 sec

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!