Create a time array

153 ビュー (過去 30 日間)
Miguel Albuquerque
Miguel Albuquerque 2022 年 6 月 30 日
回答済み: Steven Lord 2022 年 6 月 30 日
Hey guys, thanks in advance.
I have one matrix of data( this is a char data) that has two times : as visible in this figure
'Canal zero_timestamp.mat'
those numbers are: hour-minutes-seconds-miliseconds
I need to produce a time array with duration in seconds between first element of this matrix and the last element(17-11-09_923).
I wanted to conserve the miliseconds, is there anyway?

採用された回答

Steven Lord
Steven Lord 2022 年 6 月 30 日
val=['17-10-58_086'
'17-11-09_923'];
val = string(val);
Convert the string array val into a format the duration function knows how to import.
val = replace(val, '-', ':');
val = replace(val, '_', '.');
d = duration(val)
d = 2×1 duration array
17:10:58 17:11:09
The fractional seconds are present, they're just not displayed with the default display Format. We can change the Format to show them.
d.Format = 'hh:mm:ss.SSS'
d = 2×1 duration array
17:10:58.086 17:11:09.923
Now compute the difference in seconds.
seconds(diff(d))
ans = 11.8370

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2022 年 6 月 30 日
編集済み: Fangjun Jiang 2022 年 6 月 30 日
val=['17-10-58_086'
'17-11-09_923']
val = 2×12 char array
'17-10-58_086' '17-11-09_923'
StartEnd=datetime(val,'InputFormat','HH-mm-ss_SSS')
StartEnd = 2×1 datetime array
30-Jun-2022 17:10:58 30-Jun-2022 17:11:09
TimeVector=StartEnd(1):seconds(1):StartEnd(2)
TimeVector = 1×12 datetime array
30-Jun-2022 17:10:58 30-Jun-2022 17:10:59 30-Jun-2022 17:11:00 30-Jun-2022 17:11:01 30-Jun-2022 17:11:02 30-Jun-2022 17:11:03 30-Jun-2022 17:11:04 30-Jun-2022 17:11:05 30-Jun-2022 17:11:06 30-Jun-2022 17:11:07 30-Jun-2022 17:11:08 30-Jun-2022 17:11:09
datestr(TimeVector,'HH-MM-SS_FFF')
ans = 12×12 char array
'17-10-58_086' '17-10-59_086' '17-11-00_086' '17-11-01_086' '17-11-02_086' '17-11-03_086' '17-11-04_086' '17-11-05_086' '17-11-06_086' '17-11-07_086' '17-11-08_086' '17-11-09_086'

Voss
Voss 2022 年 6 月 30 日
val = [ ...
'17-10-58_086'; ...
'17-11-09_923'; ...
]
val = 2×12 char array
'17-10-58_086' '17-11-09_923'
dt = datetime(val,'InputFormat','HH-mm-ss_SSS')
dt = 2×1 datetime array
30-Jun-2022 17:10:58 30-Jun-2022 17:11:09
dt_diff = dt(end)-dt(1)
dt_diff = duration
00:00:11
% the milliseconds are present in dt_diff:
seconds(dt_diff)
ans = 11.8370
milliseconds(dt_diff)
ans = 11837

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by