user5858
user5858

Reputation: 178

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.

Upvotes: 0

Views: 1055

Answers (2)

ammportal
ammportal

Reputation: 991

Each cell you have is not a string, hence you can't use strsplit. Use this approach:

for ii = length(X) % Here X denotes your 33213168x1 cell array
    X{ii} = cell2mat(cellfun(@(y) y.', X{ii}, 'UniformOutput', false));
end

Upvotes: 0

S. Meier
S. Meier

Reputation: 51

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

Upvotes: 1

Related Questions