フィルターのクリア

Insert new rows of NaN for missing timestamps in time series.

18 ビュー (過去 30 日間)
Eric Escoto
Eric Escoto 2019 年 1 月 31 日
回答済み: Peter Perkins 2019 年 2 月 7 日
Hello, I have a time series that is missing some data. In the places where there is a data gap I would like to insert rows of NaN values at the same incremetal time step. The data is organized by a datenum.
Here is the current script I'm using to get the timeseries.
Thanks!
% Read the data.
[num, txt, raw] = xlsread('SFL_2018.xlsx'); % Creates three variables. A number (double), raw (cell), and txt (cell).
% Note that the 'txt' and 'raw' variables contain the decriptions of the columns.
% Rename the numerical matrix and remove the upper 4 rows that originally contained the data headers.
SFL2018 = num(5:end, :);
% Change the first column (excel datenum value) to MATLAB datenum value.
datevec = datetime(SFL2018(:,1),'ConvertFrom','excel');
date_num = datenum(datevec);
SFL2018 = [date_num SFL2018(:, 2:end)];
% Rename the 'txt' cell to 'header' and clear unused variables.
header = txt(1:4,:);
clearvars('num', 'txt', 'raw', 'date_num');

採用された回答

Peter Perkins
Peter Perkins 2019 年 2 月 7 日
retime "adjusts" your data to a new set of row times. In general, "adjust" means things like interpolate, or aggregate, or take the neaest value, but by default it fiills with missing values. So
>> tt = timetable([1;2;5;6],'RowTimes',datetime(2019,2,[1;2;5;6]))
tt =
4×1 timetable
Time Var1
___________ ____
01-Feb-2019 1
02-Feb-2019 2
05-Feb-2019 5
06-Feb-2019 6
>> retime(tt,datetime(2019,2,1:6))
ans =
6×1 timetable
Time Var1
___________ ____
01-Feb-2019 1
02-Feb-2019 2
03-Feb-2019 NaN
04-Feb-2019 NaN
05-Feb-2019 5
06-Feb-2019 6
Or in this simple case
>> retime(tt,'daily')
ans =
6×1 timetable
Time Var1
___________ ____
01-Feb-2019 1
02-Feb-2019 2
03-Feb-2019 NaN
04-Feb-2019 NaN
05-Feb-2019 5
06-Feb-2019 6

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2019 年 1 月 31 日
I recommend you use a timetable. Read your data in using readtable, then use table2timetable. The retime function makes what you want one line.
  1 件のコメント
Eric Escoto
Eric Escoto 2019 年 2 月 1 日
Thanks, Peter
This method doesn't seem to create new rows and insert them into the SFL2018 array, creating a new array. If it does, I don't quite get how that would happen. Could you provide an example?
-Eric

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by