Borrar filtros
Borrar filtros

How do I make an array of elapsed times?

2 visualizaciones (últimos 30 días)
Madison Lowe
Madison Lowe el 25 de Abr. de 2018
Respondida: Peter Perkins el 23 de En. de 2019
I have array, stopdates, that I need the number of seconds elapsed from t1 for each date. This is what I have been working with:
format shortg
str = 'April 18, 1995 12:00:00';
t1 = datevec(str,'mmmm dd, yyyy HH:MM:SS');
str2 = stopdate(1,runs);
t2 = datevec(str2,'mmmm dd, yyyy HH:MM:SS');
nt = etime(t2,t1);
  2 comentarios
njj1
njj1 el 25 de Abr. de 2018
What is stopdate?
Here is an easy solution that does not require etime. If you get your t# into datenum instead of datevec, then you can just subtract and cumsum everything.
%given t is a vector of datenum
t = [0;cumsum(diff(t))];
Madison Lowe
Madison Lowe el 25 de Abr. de 2018
stopdate is a 1 by 33137 array of dates in a form like str.

Iniciar sesión para comentar.

Respuestas (3)

dpb
dpb el 25 de Abr. de 2018
str = 'April 18, 1995 12:00:00';
t1 = datetime(str,'inputformat','MMM dd, yyyy HH:mm:ss');
stoptime=datetime(stopdate,'inputformat','MMM dd, yyyy HH:mm:ss');
dt=stoptime-t1;
dtSecs=seconds(dt);
Read up on datetime and duration

njj1
njj1 el 25 de Abr. de 2018
Editada: njj1 el 25 de Abr. de 2018
OK, so this sounds like it's inside a for loop, in which case you could try something like this:
for runs=number_of_runs:-1:1
%I run this backwards to ensure that my vector t is pre-allocated
t(runs) = datenum(stopdate(runs,:),'mmmm dd, yyyy HH:MM:SS');
end
%after this for loop, we have a vector t where each entry is a datenum
t = [0;cumsum(diff(t))];
Hope this helps.
  1 comentario
njj1
njj1 el 25 de Abr. de 2018
Actually, you don't need to use the for loop if all the date strings in your vector stopdate are in the same format.
t = datenum(stopdate,'mmmm dd, yyyy HH:MM:SS');
t = [0;cumsum(diff(t))];

Iniciar sesión para comentar.


Peter Perkins
Peter Perkins el 23 de En. de 2019
Unless you are using a pretty old version of MATLAB (< R2014b), use datetimes:
>> t1 = datetime('April 18, 1995 12:00:00','InputFormat','MMMM dd, yyyy HH:mm:ss')
t1 =
datetime
18-Apr-1995 12:00:00
>> t2 = datetime({'April 20, 1995 22:26:45' 'April 24, 1995 15:25:42' 'April 27, 1995 19:01:23'},'InputFormat','MMMM dd, yyyy HH:mm:ss')
t2 =
1×3 datetime array
20-Apr-1995 22:26:45 24-Apr-1995 15:25:42 27-Apr-1995 19:01:23
>> t2 - t1
ans =
1×3 duration array
58:26:45 147:25:42 223:01:23
>> between(t1,t2)
ans =
1×3 calendarDuration array
2d 10h 26m 45s 6d 3h 25m 42s 9d 7h 1m 23s

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by