how to average time at every 5 intervals in matlab

1 view (last 30 days)
Here is my data
02-Sep-2015 09:58:32: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:32: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:33: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3100E-05,5, 2.0000E-02
02-Sep-2015 09:58:34: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:35: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:35: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:35: 0, 7.3200E-05,5, 2.0000E-02
02-Sep-2015 09:58:35: 0, 7.3200E-05,5, 2.0000E-02
I have written some code
n=5;
A=arrayfun(@(i) mean(data(i:i+n-1)),1:n:length(numdata1)-n+1)';
But i find NaN as the output
can i have some suggestions
  2 Comments
sushma gunde
sushma gunde on 4 Sep 2015
@walter data is an Ascii string. the above function i used for averaging data and it worked but for time its not working..Before using the above function, i converted data to double. so it worked. when i tried to convert the time i am getting NaN as output

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Sep 2015
Starting with your character array:
datastrcell = cellstr(data);
dataparts = regexp(regexprep(datastrcell, {': (\d),', ':(\d\d),'},{':0$1,','.$1,'}),',','split');
dataparts = vertcat(dataparts{:});
timeentries = datenum(dataparts(:,1));
otherentries = str2double(dataparts(:,2:end));
data_as_num = [timeentries(:), otherentries];
Now you can apply your averaging to data_as_num. I do not code it here because I am not sure if you want to average by discrete blocks or by sliding window, and I am not sure what you want to have happen to partial blocks.
Once you have the result, call it avg_as_num, then if you want to convert it back to string format, such as
avg_data_as_str = num2str(avg_as_num(:,2:end), '%.4e,%d,%.4e');
temp_date_as_str = datestr(avg_as_num(:,1), 'dd-mmm-yyyy HH:MM:SS.fff'));
date_as_str = char( regexprep( cellstr(temp_date_as_str), {'\.0(\d)\d', '\.(\d\d)\d'}, {': $1',':$1'}) );
avg_as_str = [date_as_str, avg_data_as_str];
I have assumed here that the ": 0' is short form for ':00' and is intended to mean '.00'. I have also assumed here that it is important to restore the ": 0" form on output for the cases where the tenths of a second is 0. I also assumed that it was important that two digits of time be output. I did not, however, assume that the fractions of a second is always 0, so if there is a fraction of a second which has a non-zero tenth of a second then I leave it there but convert the '.' to ':', such as ':25' instead of '.250'.
Programs sure are less complicated when the data is in a standard format like using '.' for fractions of a second rather than ':'...
  2 Comments
sushma gunde
sushma gunde on 8 Sep 2015
@walter how to plot temp_date_as_str versus data. i am finding errors when i tried to plot
Walter Roberson
Walter Roberson on 8 Sep 2015
Do not attempt to plot the string versus the data. The date part of it would be in avg_as_num(:,1) in datenum format. You can, for example,
plot(avg_as_num(:,1), avg_as_num(:,2));
datetick('x', 'HH:MM:SS');

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!