Pregunta

I want to compute the elapsed time (in milliseconds) of data like this:

START: 2013-05-04 23:13:06.188
ENDED: 2013-05-05 1:22:41.617

I can't use etime() because I need the elapsed time in milliseconds. When I use a for loop for something like this:

[start_i, end_i] = regexp(data{i}, '\d+-\d+-\d+ \d+:\d+:\d+.\d+');
temp_str = data{i};
time{i} = cellstr(temp_str(start_i:end_i));

n1 = datenum(datevec(time{i-1}, 'yyyy-mm-dd HH:MM:SS.FFF'));
n2 = datenum(datevec(time{i}, 'yyyy-mm-dd HH:MM:SS.FFF'));

n = n2 - n1

It gives the following error:

Error using dtstr2dtvecmx
Failed on converting date string to date number.

Error in datevec (line 118)
y = dtstr2dtvecmx(t,icu_dtformat);

Error in test (line 40)
n1 = datenum(datevec(time{i-1}, 'yyyy-mm-dd HH:MM:SS.FFF'));

if I use datevec() like this:

[start_i, end_i] = regexp(data{i}, '\d+:\d+:\d+.\d+');
temp_str = data{i};
time{i} = cellstr(temp_str(start_i:end_i));

t1 = datevec(time{i-1}, 'HH:MM:SS.FFF');
t2 = datevec(time{i}, 'HH:MM:SS.FFF');

t = t2 - t1
datevec(t)

it's decreasing the time segments, element by element (seconds with seconds, minutes with minutes, etc) and give negative numbers sometimes.

I think there should be a neat way of doing this without any need of fixing the negative values manually. Does anybody know how to do that?

¿Fue útil?

Solución

How about the following:

%# your data
data = {
    'START: 2013-05-04 23:13:06.188'
    'ENDED: 2013-05-05 1:22:41.617'
};

%# extract date/time strings
s = regexprep(data, '^\w+: ', '');

%# convert to serial date number (in units of days)
t1 = datenum(s{1}, 'yyyy-mm-dd HH:MM:SS.FFF');
t2 = datenum(s{2}, 'yyyy-mm-dd HH:MM:SS.FFF');

%# difference in seconds
diff_sec = (t2-t1) * 24 * 3600
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top