使用NAN填充缺少的时间戳数据行-MATLAB [英] Populate missing timestamp data rows with NAN - MATLAB

查看:227
本文介绍了使用NAN填充缺少的时间戳数据行-MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个缺少某些时间戳的数据集.到目前为止,我已经编写了代码,

I have one dataset in which some timestamps are missing. I have written code so far as below,

x = table2dataset(Testing_data);
T1 = x(:,1);              
C1 =dataset2cell(T1);
formatIn = 'yyyy-mm-dd HH:MM:SS';
t1= datenum(C1,formatIn);

% Creating 10 minutes of time interval;
avg = 10/60/24;        
tnew = [t1(1):avg:t1(end)]';
indx = round((t1-t1(1))/avg) + 1;
ynew = NaN(length(tnew),1);
ynew(indx)=t1;

% replacing missing time with NaN
t = datetime(ynew,'ConvertFrom','datenum');                 
formatIn = 'yyyy-mm-dd HH:MM:SS';
DateVector = datevec(ynew,formatIn);
dt = datestr(ynew,'yyyy-mm-dd HH:MM:SS');
ds = string(dt);

测试数据具有此处显示的三个参数,

The testing data has three parameters shown here,

     Time                       x          y
2009-04-10 02:00:00.000         1         0.1
2009-04-10 02:10:00.000         2         0.2
2009-04-10 02:30:00.000         3         0.3
2009-04-10 02:50:00.000         4         0.4

现在您可以看到,每隔10分钟,缺少时间戳(2:20和2:40),因此我想添加该时间戳.然后,我希望xy值是NAN.所以我的输出将是这样,

Now as you can see, for intervals of 10 minutes, there are missing timestamps (2:20 and 2:40) so I want to added that time stamp. Then I want the x and y values to be NAN. So My output would be like,

       Time                     x          y
2009-04-10 02:00:00.000         1         0.1
2009-04-10 02:10:00.000         2         0.2
2009-04-10 02:20:00.000         NaN       NaN
2009-04-10 02:30:00.000         3         0.3    
2009-04-10 02:40:00.000         NaN       NaN
2009-04-10 02:50:00.000         4         0.4

从我的代码中可以看到,我只能够添加带有时间戳的NaN,但是现在我想获取其对应的x和y值.

As you can see from my code, I am just able to add NaN with time stamp but now would like to take its corresponding x and y value which I desired.

请注意,我在上述格式中有3000多个数据行,我想对所有值执行相同的操作.

Please note I have more than 3000 data rows in the above format, I want to perform the same for my all values.

推荐答案

这似乎与您的问题矛盾;您说可以插入NaN代替丢失的时间字符串,但是在预期输出的示例中,您编写了时间字符串.

it seems to be a contradiction in your question; you say tthat you are able to insert NaN in place of the missing time string but, in the example of the expected output you wrote the time string.

您还可以参考缺少的时间戳记(2:20),但是,如果时间步长为10分钟,则在示例数据中,还有另一个缺少的时间戳记(2:40)

Also you refer to missing time stamp (2:20) but, if the time step is 10 minutes, in your example data there is another missing time stamp (2:40)

假设:

  • 您实际上想插入缺少的时间st
  • 您要管理所有缺少的时间戳记

您可以如下修改代码:

  • 不需要ynew时间
  • 应使用tnew时间代替ynew
  • 要在xy列中插入NaN值,您必须:
    • dataset
    • 中提取它们
    • 创建两个新数组,将它们初始化为NaN
    • 将原始xy数据插入由indx标识的位置中
    • the ynew time is not needed
    • the tnew time should be used in place of ynew
    • to insert the NaN values in the x and y column you have to:
      • extract them from the dataset
      • create two new array initializing them to NaN
      • insert the original x and y data in the location identified by indx

      在下面的内容中,您可以找到代码的更新版本.

      In the following yu can find an updated version of your code.

      • xy数据存储在x_datay_data数组中
      • 新的xy数据存储在x_data_newy_data_new数组中
      • the x and y data are stored in the x_data and y_data array
      • the new x and y data are stored in the x_data_new and y_data_new array

      在脚本的末尾,将生成两个表:第一个表是使用时间作为string生成的,第二个表是作为单元格数组的.

      at the end of the script, two table are generate: the first one is generated using the time as string, the second one as cellarray.

      代码中的注释应标识出所做的修改.

      The comments in the code should identify the modifications.

      x = table2dataset(Testing_data);
      T1 = x(:,1);
      % Get X data from the table
      x_data=x(:,2)
      % Get Y data from the table
      y_data=x(:,3)
      
      C1 =dataset2cell(T1);
      
      formatIn = 'yyyy-mm-dd HH:MM:SS';
      t1= datenum(C1(2:end),formatIn)
      
      avg = 10/60/24;        % Creating 10 minutes of time interval;
      tnew = [t1(1):avg:t1(end)]'
      indx = round((t1-t1(1))/avg) + 1
      %
      % Not Needed
      %
      % ynew = NaN(length(tnew),1);
      % ynew(indx)=t1;
      %
      % Create the new X and Y data
      %
      y_data_new = NaN(length(tnew),1)
      y_data_new(indx)=t1
      
      x_data_new=nan(length(tnew),1)
      x_data_new(indx)=x_data
      y_data_new=nan(length(tnew),1)
      y_data_new(indx)=y_data
      
      % t = datetime(ynew,'ConvertFrom','datenum')  % replacing missing time with NAN
      %
      % Use tnew instead of ynew
      %
      t = datetime(tnew,'ConvertFrom','datenum')  % replacing missing time with NAN
      formatIn = 'yyyy-mm-dd HH:MM:SS'
      % DateVector = datevec(y_data_new,formatIn)
      % dt = datestr(ynew,'yyyy-mm-dd HH:MM:SS')
      %
      % Use tnew instead of ynew
      %
      dt = datestr(tnew,'yyyy-mm-dd HH:MM:SS')
      % ds = char(dt)
      
      new_table=table(dt,x_data_new,y_data_new)
      new_table_1=table(cellstr(dt),x_data_new,y_data_new)
      

      输出为

      new_table = 
      
              dt         x_data_new    y_data_new
          ___________    __________    __________
      
          [1x19 char]      1           0.1       
          [1x19 char]      2           0.2       
          [1x19 char]    NaN           NaN       
          [1x19 char]      3           0.3       
          [1x19 char]    NaN           NaN       
          [1x19 char]      4           0.4       
      
      
      new_table_1 = 
      
                  Var1             x_data_new    y_data_new
          _____________________    __________    __________
      
          '2009-04-10 02:00:00'      1           0.1       
          '2009-04-10 02:10:00'      2           0.2       
          '2009-04-10 02:20:00'    NaN           NaN       
          '2009-04-10 02:30:00'      3           0.3       
          '2009-04-10 02:40:00'    NaN           NaN       
          '2009-04-10 02:50:00'      4           0.4   
      

      希望这会有所帮助.

      Qapla'

      这篇关于使用NAN填充缺少的时间戳数据行-MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆