Filling missing times in timetable with NaN using retime

조회 수: 25 (최근 30일)
Louise Wilson
Louise Wilson 2022년 10월 6일
답변: dpb 2022년 10월 7일
I am trying to create rows in a timetable for days where data is missing. I have the attached table as an example.
Can anyone explain why the following does not work? Instead of getting one row filled with NaN for the only day missing data (15th Nov), all rows after the missing date are replaced with NaN.
clc;clear;clf
load('data.mat')
data.ChunkEnd(4:end)=data.ChunkEnd(4:end)+seconds(1); %think this is necessary because the timing is slightly off?
data_retime=retime(data,data.ChunkEnd(1):days(1):max(data.ChunkEnd),'fillwithmissing');
  댓글 수: 1
Louise Wilson
Louise Wilson 2022년 10월 6일
I can get it to work if I do
data.ChunkEnd=data.ChunkEnd-timeofday(data.ChunkEnd)
data_retime=retime(data,data.ChunkEnd(1):days(1):max(data.ChunkEnd),'fillwithmissing');
since the time isn't actually necessary in this context, but I'm wondering why it doesn't work...

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

답변 (1개)

dpb
dpb 2022년 10월 7일
load(websave('data.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1148080/data.mat'))
ttD=data_site_num_tt1; % just get a shorter variable name to work with
clear data_site_num_tt1;
rule=-1*double(second(ttD.ChunkEnd)==0); % don't round up 00 seconds rule
ttD.ChunkEnd=dateshift(ttD.ChunkEnd,'end','minute',rule); % round up any those with seconds
newt=datetime(ttD.ChunkEnd(1):days(1):ttD.ChunkEnd(end)); % fill in the time vector to match
ttD=retime(ttD,newt) % and retime -- will insert missing automagically
ttD = 8×5 timetable
ChunkEnd podN DPM Nall MinsOn FalsePositiveDPM ____________________ ____ ___ __________ ______ ________________ 12-Nov-2017 23:59:00 2714 7 1756 1440 0 13-Nov-2017 23:59:00 2714 16 11348 1440 0 14-Nov-2017 23:59:00 2714 0 3310 1440 0 15-Nov-2017 23:59:00 NaN NaN NaN NaN NaN 16-Nov-2017 23:59:00 1533 0 32098 1440 0 17-Nov-2017 23:59:00 1533 0 48293 1440 3 18-Nov-2017 23:59:00 1533 0 3.4973e+05 1440 0 19-Nov-2017 23:59:00 1533 0 18702 1440 0
The above is a little esoteric with the use of the secondary rule argument and the logic test to set it so would not turn 23:59 into 24:00 and then 00:00 of the next day, granted, but figured may as well demonstrate a corner feature of retime while at it and had the opportunity.
One might just use regular timing and round to the next day since all are so near midnight; that's a decision the data owner has to decide about which date to report on.

카테고리

Help CenterFile Exchange에서 Timetables에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by