Write missing data as NaN
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2018-7-16
编辑:dpb
2018-7-16
OK, without timetable and retime...
dt=(datetime(data(1,1:6)):minutes(15):datetime(data(end,1:6))).'; % build the full time vector
t=table(dt,nan(size(dt)),'VariableNames',{'Time','Rain'}); % empty table
ix=ismember(t.Time,datetime(data(:,1:6))); % data available locations
t.Rain(ix)=data(:,end) % and insert...
t =
5×2 table
Time Rain
____________________ ____
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 个评论
dpb
2018-7-16
U're welcome, as always, "more than one way to skin..." :)
Note one could do the same thing with the array by converting back via datevec if need be but the table is really a useful data structure as long as doesn't get too large that performance begins to lag.
更多回答(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 个评论
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.
Adam Danz
2018-7-16
编辑:Adam Danz
2018-7-16
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);
4 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!