MATLAB: Cell2mat – problem with consistency of matrices

cell2mat

Hello everyone,
I would have a question regarding the cell2mat function. My data are saved in a cell file with this kind of structure:
val(:,:,1) =
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
val(:,:,2) =
[4x1 double] [4x1 double] [4x1 double] [4x4 double] [4x4 double]
[] [] [] [] []
[] [] [] [] []
[] [] [] [] []
[] [] [] [] []
[] [] [] [] []
etc.
I would like to have acces to what is in the file, namely, the following data:
dataAss{ss,1,bloc} = ones(length(sequence(1,:)),1)*bloc; dataAss{ss,2,bloc} = sequence(2,:)'; dataAss{ss,3,bloc} = sequence(1,:)'; dataAss{ss,4,bloc} = actionsExecuted; dataAss{ss,5,bloc} = soundProduced;
save(fileNameAPPR, 'dataAss');
PS: "Ass" = Association, not the other meaning…
So, I wrote cell2mat(dataAss), but I have the following error: Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 125) m = cat(1,c{:});
Not sure I understand the mistake because I don't see the inconsistency.
Thank you for the help!
Emilie

Best Answer

It appears that you are trying to use cell2mat to concatenate contents of the cell array val into one numeric matrix, but the numeric array dimensions are not suitable for such concatenation.
If you read the documentation for cell2mat, you will find this rather important information: "The contents of C must support concatenation into an N-dimensional rectangle. Otherwise, the results are undefined. For example, the contents of cells in the same column must have the same number of columns, although they need not have the same number of rows".
Older versions used to state this with slightly more detail: "Moreover, for each pair of neighboring cells, the dimensions of the cells' contents must match, excluding the dimension in which the cells are neighbors".
Let us now look at your cell array val: the dimensions of element {2,1,1}==[4,1] whereas the dimension of {2,1,2}==[0,0]. How do you expect these two elements to be concatenated together along the third dimension? The third dimension size is irrelevant (according to the quote above), but "the dimensions of the cells' contents must match", which clearly is not true for your attempt: [4,1]~=[0,0].
There are a few questions that you need to answer in order for us to know how to help you any further:
  • Why are the data in cell arrays anyway?
  • Why not save them in the cell array?
  • What further processing are you planning for this data?