在MATLAB中对多行执行字符串拆分 [英] Do a string split for more than one row in MATLAB
问题描述
我写了一个for循环,用于沿着它们所在的每一列相应地拆分5000行。
I have written a for loop in which to split 5000 rows accordingly along each of the columns that they are in.
包含这些行的单元格数组示例:
Example of the cell array that contains those rows:
从这张照片,我想分开每一行相应的行从第一列到结束的那一行的相应列。
From that picture, i would like to split each row accordingly along their respective columns of that row starting from the first column to the end.
这是我的代码写为:
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
我使用正则表达式替换冒号(:),用空格。但是,它抛出一个错误,说明输入字符串必须有一行。
。我不知道如何解决这个问题。
I have used Regular Expressions to replace the colons (:), with a whitespace. However, it is throwing me an error stating Input strings must have one row.
. I don't know how to solve this.
请指教。
推荐答案
我会这样做:
%Some sample data
data = {'1 : 2 : 3 :4: 5: 6';'7 :8 : 9: 10 :11 :12'};
根据分隔符划分所有行(分隔符是空格和: )
The Divide all the rows based on delimiters (a delimiter is any combinations of white space and ":")
splitData = regexp(data,'[\s\:]*','split')
现在您的拆分数据可以读取为
Now your split data can be read out as
example = splitData{row}{column};
很可能你想将它转换为数字(不是字符串)。您可以这样一次执行一行:
Most likely you will want to convert this to numbers (not strings). You can do this one row at a time like this:
numericRow = num2double(splitData{rowNumber});
这篇关于在MATLAB中对多行执行字符串拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!