cell2mat - problem with consistency of matrices

1 view (last 30 days)
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
  1 Comment
Stephen23
Stephen23 on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
What is a "cell file"? MATLAB does not have any data class "cell file", and does not (by default) handle any file formats known as "cell file".
Are you referring to either of:
  • A cell array , one of the standard data classes in MATLAB, or
  • a .mat file, which can be used to store workspace data, including cell arrays ?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
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?
  1 Comment
Stephen23
Stephen23 on 10 Jan 2015
Edited: Stephen23 on 10 Jan 2015
This can also be visualized quite easily. Consider a plane of the cell array along the third dimension:
val(:,1,:) =
[4x1 double] [4x1 double] ...
[4x1 double] [] ...
[4x1 double] [] ...
[4x1 double] [] ...
[4x1 double] [] ...
[4x1 double] [] ...
How should these joined into one array? The first column would generate a numeric array of size 24x1, the second column an array of size 4x1. These cannot be concatenated horizontally, ergo the error.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!