【问题标题】:Do a string split for more than one row in MATLAB在 MATLAB 中对多行进行字符串拆分
【发布时间】:2012-03-11 03:12:12
【问题描述】:

我编写了一个 for 循环,在其中沿它们所在的每一列相应地拆分 5000 行。

包含这些行的元胞数组示例:

从那张图片中,我想从第一列到最后,沿着该行各自的列相应地拆分每一行。

这是我写的代码:

for i = pdbindex(:,1)

    clean_pdb = regexprep(pdbindex, ':', ' '); % removes the colon (:) from the array and replaces it with a whitespace
    pdb2char = char(clean_pdb); % converts the cell array into a character array
    pdb2split = strsplit(pdb2char, ' '); % does a split based on the character array followed by a delimiter, which is the white space

end

我使用正则表达式将冒号 (:) 替换为空格。但是,它给我一个错误,说明Input strings must have one row.。我不知道如何解决这个问题。

请指教。

【问题讨论】:

  • 看起来您的第一行是空的。试试for i = pdbindex(2:end,1)
  • 我很抱歉。我已经对我的单元格数组进行了修改。在测试了 for 循环后,我将鼠标光标移到该行上并按 Enter。

标签: matlab for-loop char strsplit


【解决方案1】:

我会这样做:

%Some sample data
data = {'1 : 2  :  3 :4: 5: 6';'7 :8 : 9: 10 :11 :12'};

根据分隔符划分所有行(分隔符是空格和“:”的任意组合)

splitData = regexp(data,'[\s\:]*','split')

现在你的分割数据可以被读出为

example = splitData{row}{column};

您很可能希望将其转换为数字(而不是字符串)。您可以一次执行此操作,如下所示:

numericRow = num2double(splitData{rowNumber});

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 2016-07-03
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2016-12-31
    相关资源
    最近更新 更多