Search code examples
matlabsplitcell-array

separating cell array into several columns MATLAB


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'   []
         }

Solution

  • 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