Do a string split for more than one row in MATLAB

北城余情 提交于 2019-12-24 19:18:57

问题


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.

This is the code that i have written:

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.

Please advise.


回答1:


I would do this like 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});


来源:https://stackoverflow.com/questions/9366887/do-a-string-split-for-more-than-one-row-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!