Write missing data as NaN
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
dpb
am 16 Jul. 2018
Bearbeitet: dpb
am 16 Jul. 2018
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 Kommentare
dpb
am 16 Jul. 2018
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.
Weitere Antworten (2)
dpb
am 16 Jul. 2018
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 Kommentare
dpb
am 16 Jul. 2018
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
am 16 Jul. 2018
Bearbeitet: Adam Danz
am 16 Jul. 2018
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 Kommentare
Siehe auch
Kategorien
Mehr zu Time Series finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!