大矩阵中的空单元格的问题:MATLAB

时间:2019-03-21 05:37:01

标签: matlab matrix

我有一个102,730行的大型矩阵,其形式为文本文件(附加了示例文本文件),其中带有一些头文件。第一列显示年份,第二个月显示月份,其次是日期,以及值1,值2和值3。某些单元格缺失/为空。我想用NaN填充这些空单元格,以便它们不会与下一个值保持一致。

这是输入矩阵:

1970 01 13  21.0   6.1 06 000.0
1970 01 14  22.4   8.1 03 000.0
1970 01 15  21.2   8.1 04 000.0
1970 01 16  22.6   9.1 04 000.0
1970 01 17  22.8   9.1 02 000.0
1970 01 18  22.9   8.9 07 000.0
1970 01 19  23.8  10.8 04 000.0
1970 01 20  21.8  12.1 10 010.5
1970 01 21  19.8       06 012.9
1970 01 22  15.3   8.5 07 000.0
1974 06 28  39.2  25.6 03 000.0
1974 06 29  41.2  30.5 05 000.0
1974 06 30  40.3  31.2 07 000.0
1974 07 01  41.3  31.5 12 000.0
1974 07 02  43.3  31.3 20 000.0
1974 07 03  41.2       16 041.6
1974 07 04  34.3  21.4 14 054.5
1974 07 05  33.1  23.8 05 000.0
1974 07 06  36.2  28.9 06 000.0
1975 04 18  36.6  20.8 12 000.0
1975 04 19  37.4  21.1 05 000.0
1975 04 20  39.9  27.0 07 000.0
1975 04 21  39.5  27.3 09 000.0
1975 04 22                     
1975 04 23  39.5  27.1 08 000.0
1975 04 24  37.7  26.0 10 000.0
1975 04 25  38.7  27.2 15 000.0

所需的输出矩阵:

1970 01 13  21.0   6.1 06 000.0
1970 01 14  22.4   8.1 03 000.0
1970 01 15  21.2   8.1 04 000.0
1970 01 16  22.6   9.1 04 000.0
1970 01 17  22.8   9.1 02 000.0
1970 01 18  22.9   8.9 07 000.0
1970 01 19  23.8  10.8 04 000.0
1970 01 20  21.8  12.1 10 010.5
1970 01 21  19.8  Nan  06 012.9
1970 01 22  15.3   8.5 07 000.0
1974 06 28  39.2  25.6 03 000.0
1974 06 29  41.2  30.5 05 000.0
1974 06 30  40.3  31.2 07 000.0
1974 07 01  41.3  31.5 12 000.0
1974 07 02  43.3  31.3 20 000.0
1974 07 03  41.2  Nan  16 041.6
1974 07 04  34.3  21.4 14 054.5
1974 07 05  33.1  23.8 05 000.0
1974 07 06  36.2  28.9 06 000.0
1975 04 18  36.6  20.8 12 000.0
1975 04 19  37.4  21.1 05 000.0
1975 04 20  39.9  27.0 07 000.0
1975 04 21  39.5  27.3 09 000.0
1975 04 22  Nan   Nan  Nan Nan                     
1975 04 23  39.5  27.1 08 000.0
1975 04 24  37.7  26.0 10 000.0
1975 04 25  38.7  27.2 15 000.0

作为尝试,首先我尝试了以下方法:

T = readtable('sample.txt') ;

上面的代码不起作用,因为它啮合在一起,并且小数点前有2位数字时给出了错误的列数。其次,我找到了此链接:Creating new matrix from cell with some empty cells disregarding empty cells

愚弄。该代码段可能对此链接有用,但我不知道如何直接从文本板读取数据以应用此代码和后续的检索过程:

inds = ~cellfun('isempty', elem); %elem to be replaced as sample

我还在这里找到了检测空单元格的方法:How do I detect empty cells in a cell array?

但是考虑到这些空单元格,我不知道如何从文本文件中读取数据。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

R2019a 起,您只需使用readmatrix

>> myMat = readmatrix('sample.txt')

从文档中

  

对于定界文本文件,导入功能会将文件中的空白字段转换为NaN(对于数字变量)或空白字符向量(对于文本变量)。文本文件中的所有行都必须具有相同数量的定界符。导入功能会忽略文件中无关紧要的空白。



对于以前的版本,可以在调用detectImportOptions时使用readtable对象:

% Detect options.
>> opts = detectImportOptions('sample.txt');
% Read table.
>> myTable = readtable('sample.txt',opts);
% Visualise last rows of table.
>> tail(myTable)

ans =

  8×7 table

    Var1    Var2    Var3    Var4    Var5    Var6    Var7
    ____    ____    ____    ____    ____    ____    ____

    1975     4       18     36.6    20.8     12       0 
    1975     4       19     37.4    21.1      5       0 
    1975     4       20     39.9      27      7       0 
    1975     4       21     39.5    27.3      9       0 
    1975     4       22      NaN     NaN    NaN     NaN 
    1975     4       23     39.5    27.1      8       0 
    1975     4       24     37.7      26     10       0 
    1975     4       25     38.7    27.2     15       0 

对于您的文本文件,detectImportOptionsNaN填充缺失值:

>> opts.VariableOptions

如果所需的输出是矩阵,则可以使用table2array

>> myMat = table2array(myTable)