Search code examples
arraysmatlabsplitdelimitercell-array

Matlab- split cell array column by delimiter


I have many 33213168x1 cell arrays, where each cell contains an 85 x 1 column.

Each cell in the column is in the form

[0.55;0.25;0.75]

[0.33;0.66;0.99]

I want to split up this single column by the semi-colon delimiter so that each cell in the cell array is 85x3, like:

[0.55][0.25][0.75]

[0.33][0.66][0.99]

I've tried numerous techniques to solve this, but most commonly get the errors 'cell elements must be character arrays' or 'input must be a string.'

Some of the approaches I've tried:

splitcells = strsplit(regress_original_053108,';');
splitcells = cellfun(@(x) strsplit(regress_original_053108, ';'),regress_original_053108 , 'UniformOutput',0);
splitcells = regexp(regress_original_053108, ';', 'split');
splitcells = textscan(regress_original_053108, 'delimiter', ';');

Etc. Any feedback about how to do this would be appreciated.


Solution

  • Hope this solves your problem:

    % Example input
    input = {[0.55;0.25;0.75]};
    cellArray(1:85,1) = input;
    
    % Create array
    doubleArray = zeros(85,3);
    
    % Fill array
    for i=1:85
       doubleArray(i,:) =  cellArray{i,1}';
    end