【问题标题】:insert a space/tab to separate field in a column element MATLAB在列元素 MATLAB 中插入空格/制表符以分隔字段
【发布时间】:2016-09-26 09:46:31
【问题描述】:

我有一个矩阵如下:

615319419701102123000000 000000 000000 000000 000000 000000 000000 000003 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000

矩阵有 24 列。在第 1 列中,7 位数字表示(即 6153194)站 ID,接下来的 4 位数字表示年份(1970),接下来的 2 位数字表示月份(11 即 11 月),接下来的 2 位数字表示月份中的日期(例如在 第 1 列 02 表示第 2 天),然后 123 是标志,表示它的降水率时间序列和最后六位数字(即,000000 是降雨量)。最后 23 列表示以毫米为单位的每小时降雨量数据。

我想将第 1 列的字段分隔为站 ID、年、月、日和值以输入到程序中,如下所示:

6153194 1970 11 02 123 000000 000000 000000 000000 000000 000000 000000 000003 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000

最后还有一个时间序列:

6153194 1970    11  2   123 0
6153194 1970    11  2   123 0
6153194 1970    11  2   123 0
..................
.................
6153194 1970    11  2   123 0
6153194 1970    11  2   123 0

我主要关心的是如何使用制表符/空格将字段与第一列分开,然后如何构建一个时间序列?对此有任何帮助/建议

【问题讨论】:

    标签: matlab matrix time-series


    【解决方案1】:

    下面的代码应该会产生您想要产生的输出。但是,如果我完全理解您的问题,我并不完全确定。如果您需要不同格式的数据,请告诉我,我会修改代码。

    运行代码后,charMatrix 将包含数据作为字符数组,其中第一列分隔,如您问题中的示例所示。 numMat 是一个数组,其中包含与charMatrix 对应的数值数据。

    %% Subdivision of the first column.
    
    % Read the data.
    fid = fopen('file.txt');
    charCell = textscan(fid, '%s', 'delimiter', '\n');
    charCell = charCell{1};
    
    % Split by whitespace.
    charCell = cellfun(@strsplit, charCell, 'UniformOutput', false);
    
    % Split the first column.
    charCell = cellfun( ...
        @(charMatrixRow) { ...
        charMatrixRow{1}(1 : 7) ...
        charMatrixRow{1}(8 : 11) ...
        charMatrixRow{1}(12 : 13) ...
        charMatrixRow{1}(14 : 15) ...
        charMatrixRow{1}(16 : 18) ...
        charMatrixRow{2 : end} ...
        }, ...
        charCell, ...
        'UniformOutput', false ...
        );
    
    % Join individual columns with a whitespace delimiter.
    charMatrix = cellfun( ...
        @strjoin, charCell, 'UniformOutput', false ...
        );
    
    % If you would like to get the values in the form of a character array:
    charMatrix  = cell2mat(charMatrix);
    
    %% Numeric representation of the columns.
    % If you would like to get the individual columns in the form of numeric
    % arrays, use the code below.
    
    charCell = vertcat(charCell{:});
    
    numMat = cell2mat(cellfun(@str2num, charCell, 'UniformOutput', false));
    

    【讨论】:

    • 您好,感谢您的帮助!数据是数字格式,不是字符数组的形式。我可以使用文本/记事本中的 importdata 命令直接打开它。在那种情况下,我会遇到错误。行:charCell = cellfun(@strsplit, charCell, 'UniformOutput', false);
    • 感谢您的评论。我认为,不知何故,您需要将数据转换为charMatrix 中指定形式的字符数组,因为否则,您将在存储原始数据集的第一列时遇到精度问题。 MATLAB 支持的最大整数是 18446744073709551615 (uint64)。如果您可以上传您的数据文件,我可以提供完整的解决方案。
    • 嗨,我尝试使用文本扫描读取数据:X = textscan(fileID,'%s',24,'Delimiter','\n');有 24 列,所以我把 24 放在这里。然后我尝试使用 char 命令将单元格数组转换为字符数组。我收到一个错误“单元格元素必须是字符数组”。有什么建议我应该如何进行?
    • 我已保存在 googledocs 中,因为我不知道您的电子邮件 ID。如果您可以访问,请告诉我:docs.google.com/document/d/…
    • 我修改了从文件中读取的代码。我认为您遇到的问题与 textscan 的输出格式有关 - 请参见代码中的第 6 行。
    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2017-08-11
    • 2015-08-02
    相关资源
    最近更新 更多