how to average time at every 5 intervals in matlab
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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 Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!