0
votes

I have a cell array (16x5) and I would like to extract all the values held in each column of the cell array and place them into a column within a matrix such that the columns are preserved (i.e. new matrix column for each cell array column).

What is the best way to do this?

I have tried:

for k=1:Samples
data(k,:) = [dist{:,k}];
end

But this returns the error

Subscripted assignment dimension mismatch.

However I am not sure why.

EDIT - Cell array structure: enter image description here

1
What is the content of your cell array elements (scalars, double arrays, etc)? - TroyHaskin
How can you check the data type of a cell array? 'whos' only defines it as a cell. I've updated the OP to include a screenshot of the cell array. - AnnaSchumann
The output must be a cell array, right? Because otherwise those irregular shaped cell elements won't fit into a numeric array. And if, the output has to be a cell array, then it would be a 1 x 5 sized cell array, right? - Divakar

1 Answers

1
votes

Since your loop code is valid, I assume the error is being raised because data is preallocated with dimensions not matching the length of the comma-expanded dist column (Matlab will grow matrices with explicit indices but not with the : operator). You just need to get the length of the data after the comma-separated expansion:

nElem   = numel([dist{:,1}]);
Samples = size(dist,2);
data    = zeros(Samples,nElem);  
for k=1:Samples
  data(k,:) = [dist{:,k}];
end

Or if you want it in columns

data = zeros(nElem,Samples);  
for k=1:Samples
  data(:,k) = [dist{:,k}]';
end