handle character minus character

5 visualizzazioni (ultimi 30 giorni)
Ancalagon8
Ancalagon8 il 10 Lug 2020
Commentato: madhan ravi il 10 Lug 2020
I have two characters which contain '18:29:42.835' and '18:29:49.425'. How is it possible to subtrack t2-t1 so as to find duration?

Risposta accettata

madhan ravi
madhan ravi il 10 Lug 2020
f = @(x) str2double(regexp(x,':','split'));
t1 = num2cell(f(t1)); % where t1 & 2 are your char arrays
t2 = num2cell(f(t2));
Wanted = duration(t2{:})-duration(t1{:})
  2 Commenti
Ancalagon8
Ancalagon8 il 10 Lug 2020
thanks it worked! But it only shows me seconds and i need also the difference in decimals..
madhan ravi
madhan ravi il 10 Lug 2020
f = @(x) str2double(regexp(x,'\D','split'));
t1 = num2cell(f(t1));
t2 = num2cell(f(t2));
Wanted = duration(t2{:})-duration(t1{:});
Wanted.Format = 'hh:mm:ss.SSSS'

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 10 Lug 2020
If you have a recent enough MATLAB version, pass the character vectors to duration() and then subtract one from the other.
If your MATLAB is a bit older, then you will need to split the vectors at the colons, then str2double() to get the individual components, and pass those to duration.
Or you could use sscanf() with a format of '%f:%f:%f' to get the numeric components and pass those to duration()

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by