how to average time at every 5 intervals in matlab

조회 수: 1 (최근 30일)
sushma gunde
sushma gunde 2015년 9월 3일
댓글: Walter Roberson 2015년 9월 8일
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
sushma gunde
sushma gunde 2015년 9월 4일
@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

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 4일
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
sushma gunde
sushma gunde 2015년 9월 8일
@walter how to plot temp_date_as_str versus data. i am finding errors when i tried to plot
Walter Roberson
Walter Roberson 2015년 9월 8일
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');

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by