Write missing data as NaN
이전 댓글 표시
I have multiple rain time series with 15 minute interval, but with some missing datas, like this:
yyyy mm dd hh mm ss data
2000 01 30 11 00 00 3.00
2000 01 30 11 15 00 2.00
2000 01 30 11 45 00 0.00
2000 01 30 12 00 00 0.00
And I need to add the missing datas with NaN at data row, resulting this
yyyy mm dd hh mm ss data
2000 01 30 11 00 00 3.00
2000 01 30 11 15 00 2.00
2000 01 30 11 30 00 NaN
2000 01 30 11 45 00 0.00
2000 01 30 12 00 00 0.00
There's some way to do that in MatLab?
Tks
댓글 수: 4
Adam Danz
2018년 7월 16일
Q1) How are these data stored? -in individual vectors? -in a matrix? -a cell array?
Q2) are only the ss data missing or could any part of the time stamp be missing? What's in its place? -a blank? I don't see any missing data in your example at the top.
Danilo M
2018년 7월 16일
Adam Danz
2018년 7월 16일
Got it. Q3) What's the last element of each row ('data')?
Danilo M
2018년 7월 16일
채택된 답변
추가 답변 (2개)
dpb
2018년 7월 16일
tt=timetable(datetime(data(:,1:6)),data(:,end));
tt.Properties.VariableNames={'Data'};
tt=retime(tt,tt.Time(1):minutes(15):tt.Time(end))
tt =
5×1 timetable
Time Data
____________________ ____
30-Jan-2000 11:00:00 3
30-Jan-2000 11:15:00 2
30-Jan-2000 11:30:00 NaN
30-Jan-2000 11:45:00 0
30-Jan-2000 12:00:00 0
댓글 수: 2
Danilo M
2018년 7월 16일
dpb
2018년 7월 16일
Bummer! About first time found it to be useful adjunct... :) Unfortunately, retime came along with it also in R2016b. Can't update, I suppose?
Something similar to the other solution is the way although probably some shorter paths to the same end are possible.
Here's another method that keeps the data in matrix format but dpb's answer is quicker and more direct.
This method creates a list of all possible time stamps between two bounds given a sample rate. Then it assigns NaN data to each time stamp, finds the time stamps you've got, and fills in the data you got.
startDate = '01/30/2000 11:00:00';
endDate = '01/1/2001 12:00:00';
sampleRate = '00:15:00'; %every 15 min
% Create all possible time stamps
allTimeStamps = datetime(startDate, 'Format', 'MM/dd/yyyy HH:mm:ss') : ...
minutes(15) : datetime(endDate, 'Format', 'MM/dd/yyyy HH:mm:ss');
% Convert to matrix of time-vectors
allTimeStamps = datevec(allTimeStamps');
% Create your (fake) rain data and time stamps
rainData = [allTimeStamps, randi(10, size(allTimeStamps,1),1)];
% Remove some random rows
idx = randi(size(rainData,1),10, 1); %randomized row numbers to remove.
rainData(idx, :) = []; %now we have missing data.
% Detect which time stamps in 'rainData' are in 'allTimeStamps'
matchIdx = ismember(allTimeStamps, rainData(:,1:end-1), 'rows');
% add all NaNs to final column of allTimeStamps, then fill in the data you've got.
allTimeStamps = [allTimeStamps, nan(size(allTimeStamps,1),1)];
allTimeStamps(matchIdx, end) = rainData(:,end);
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!