separating cell array into several columns MATLAB

拥有回忆 提交于 2019-12-11 05:36:30

问题


I have a cell array with one column. Each row consists of only one column. Each cell consist of a String. How can I separate the content of one column in cell array into several columns by separating the string based on space. Each string has different length. Example:

cellArrayM= {
  'hh pp'
  'my 2 ewr 3234 csdf'
  'input l 34'
  'output K 99 100'
 }

result={
   'hh'     'pp' []    []     []
   'my'     '2' 'ewr' '3234' 'csdf'
   'input'  'l' '34'  []      []
   'output' 'k' '99'  '100'   []
         }

回答1:


You can do it this way:

x = cellfun(@(x) strsplit(x), cellArrayM, 'uniformoutput', 0);
result = cell(numel(x), max(cellfun(@numel, x)));
for k = 1:numel(x)
    result(k, 1:numel(x{k})) = x{k};
end



回答2:


You can do like this:

splitCellArray = regexp(cellArrayM,' ','split')


来源:https://stackoverflow.com/questions/45292709/separating-cell-array-into-several-columns-matlab

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