필터 지우기
필터 지우기

How can I detect gaps in a time series matrix and insert NaN's

조회 수: 3 (최근 30일)
Hi, I have a time series matrix with the date and time in separate columns and I need to find the gaps and fill them in with NaN's. By looking at other suggestions through the File Exchange, I figured out how to find the gaps using:
sp=1; %sampling period is every hour
t=continuouswind3(:,3);
idx=find(diff(t)>(2*sp))'+1; %to detect gaps greater than twice the sampling period
Since my date and time stamps are in individual columns [YYYY MM DD hh mm] I have to first find the missing minutes (10 min time stamp), then hour (1 hour), then days (1 day). From running the script several times changing the columns, it appears that I am not missing any months. I now need to fill in the missing data in columns 6 through 10 with NaN's and the corresponding missing time stamps in columns 1 through 5. I am working with wind data measured once every 10 min from January 1 2006 through December 31 2014. Thank you!!!!
  댓글 수: 2
Image Analyst
Image Analyst 2015년 2월 4일
Attach a .mat file with continuouswind in it, and a screenshot of your plot.

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

채택된 답변

Image Analyst
Image Analyst 2015년 2월 4일
To replace the indexes specified by idx with NANs, do this;
t(idx) = nan;
  댓글 수: 1
Janet Reimer
Janet Reimer 2015년 2월 5일
The index only gives me where the gap starts. In most cases the gap is much longer. Would you recommend a For cycle?

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

추가 답변 (1개)

David Young
David Young 2015년 2월 4일
편집: David Young 2015년 2월 4일
I think this will do what you want:
% test data
data = [2015 01 20 01 10 1 2 3 4 5; ...
2015 01 20 01 20 1 2 3 4 5; ... 20 min gap
2015 01 20 01 40 1 2 3 4 5; ...
2015 01 20 01 50 1 2 3 4 5; ... 1hr 10 min gap
2015 01 20 03 00 1 2 3 4 5; ... 30 min gap
2015 01 20 03 30 1 2 3 4 5];
% parameter in days
timestep = 10 / (24 * 60); % 10 minute increment
% convert time array to simple vector of datenumbers
timevecs = [data(:, 1:5) zeros(size(data,1), 1)];
timestamps = datenum(timevecs);
% Round to get index into output array. This assumes that all the times in
% the data are close to multiples of the timestep after the first one. It
% would be easy to check the assumption at this point if there is any
% doubt.
indexes = 1 + round((timestamps - timestamps(1))/timestep);
% Create output array correct size
nfull = indexes(end);
fulldata = NaN(nfull, size(data,2));
% populate the first 5 columns with the new dates
fulltimestamps = timestamps(1) + timestep * (0:nfull-1);
fulltimevecs = datevec(fulltimestamps);
fulldata(:, 1:5) = fulltimevecs(:, 1:5);
% populate the last columns with the original data
fulldata(indexes, 6:end) = data(:, 6:end);
  댓글 수: 2
Janet Reimer
Janet Reimer 2015년 2월 5일
Thank you! That did it. Reading the code it all seems so simple now.
Alexandre Canitano
Alexandre Canitano 2019년 2월 20일
Great code, thanks for sharing.

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by