# Conversion from d:h:m:s:ms to seconds

5 Ansichten (letzte 30 Tage)
Calum am 12 Mai 2023
Beantwortet: Atsushi Ueno am 12 Mai 2023
Hi folks, I've been given a large data array of timetags that I would like to change to seconds but just don't have the knowledge to be able to do so yet.
I've managed to extract this field's data from a structure, giving me a large 10000x1 cell array of data that looks exactly like this:
%e.g. sample data
%.
%.
'104d:11h:59m:38.934815s'
'104d:11h:59m:38.936032s'
'104d:11h:59m:39.014802s'
'104d:11h:59m:39.016018s'
%.
%.
From this I would like to be able to convert the HOURS, MINUTES, SECONDS and MILLISECONDS all into seconds format to be able to perform calculations with these, negating the days.
Hope someone can help!
Thanks,
C.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 12 Mai 2023
Bearbeitet: Stephen23 am 12 Mai 2023
"negating the days."
I guess you really mean to ignore the days.
format long G
C = {'104d:11h:59m:38.934815s';'104d:11h:59m:38.936032s';'104d:11h:59m:39.014802s';'104d:11h:59m:39.016018s'}
C = 4×1 cell array
{'104d:11h:59m:38.934815s'} {'104d:11h:59m:38.936032s'} {'104d:11h:59m:39.014802s'} {'104d:11h:59m:39.016018s'}
Method one: SSCANF and matrix multiplication:
M = sscanf([C{:}],'%fd:%fh:%fm:%fs',[4,Inf]).';
V = M * [0;60*60;60;1] % seconds
V = 4×1
1.0e+00 * 43178.934815 43178.936032 43179.014802 43179.016018
Method two: SPLIT, EXTRACTBEFORE, STR2DOUBLE, matrix multplication:
M = str2double(extractBefore(split(C,':'),lettersPattern));
V = M * [0;60*60;60;1] % seconds
V = 4×1
1.0e+00 * 43178.934815 43178.936032 43179.014802 43179.016018
Method three: REPLACE and EXTRACTAFTER and DURATION and SECONDS:
V = seconds(duration(extractAfter(replace(C,lettersPattern,''),':')))
V = 4×1
1.0e+00 * 43178.934815 43178.936032 43179.014802 43179.016018
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Calum am 12 Mai 2023
Thanks so much! This is exremely helpful and gives me lots of options to explore for the future!

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Atsushi Ueno am 12 Mai 2023
format long % to display milli seconds
DateTimeStr = {'104d:11h:59m:38.934815s','104d:11h:59m:38.936032s','104d:11h:59m:39.014802s','104d:11h:59m:39.016018s'};
DateTimeStr = regexprep(DateTimeStr,'\d+d:',''); % negating the days
times = datetime(DateTimeStr,"Format","HH'h:'mm'm:'ss.SSSSSS's'")
times = 1×4 datetime array
11h:59m:38.934815s 11h:59m:38.936032s 11h:59m:39.014802s 11h:59m:39.016018s
scnds = hour(times).*3600 + minute(times).*60 + second(times);
seconds(scnds) % convert the HOURS, MINUTES, SECONDS and MILLISECONDS all into seconds format
ans = 1×4 duration array
43178.934815 sec 43178.936032 sec 43179.014802 sec 43179.016018 sec
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by