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

问题描述 投票:2回答:1

我有一个102730行的大矩阵,以文本文件的形式(附加了示例文本文件),其中包含一些头文件。第一列显示年份,下一个月,然后是日期,以及value1,value2和值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?

但考虑到这些空单元格,我无法弄清楚如何从文本文件中读取数据。

有人可以帮忙吗?

matlab matrix
1个回答
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 

对于您的文本文件,detectImportOptions使用NaN填充缺失值:

>> opts.VariableOptions

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

>> myMat = table2array(myTable)
© www.soinside.com 2019 - 2024. All rights reserved.