i need to convert a cell with different datatype to a matrix please any one help me to do this

i am having a cell vector y= [16x16 double] {1x3 cell} {1x3 cell} {1x16 cell} {1x32 cell} {1x32 cell} i need to convert this to matrix i have used the cell2mat command but it shows the error that "All contents of the input cell array must be of the same data type". please any one help me to do this.

1 Comment

Well, you seem to have an issue with the dimension of the objects that are stored in your cells, that will prevent any vertcat/horzcat anyway. Assuming that you could perform cell2mat the way you tried, how should these objects concatenate?
It seems that the only way for it to make sense dimension-wise, is if each cell of internal cell arrays is a 16xn numeric array in itself, with arbitrary n, and that you want to concatenate them horizontally ..

Sign in to comment.

 Accepted Answer

If you always have this structure with a numeric array first and then cell arrays, and if the second part of my comment above is true, then you can do the following:
y_num = [y{1}, cell2mat([y{2:end}])] ;
Example:
>> y = { [1 2 3; 4 5 6; 7 8 9], {[10 11 12].',[13 14 15].'}, {[16 17 18].'}}
y =
[3x3 double] {1x2 cell} {1x1 cell}
>> y_num = [y{1}, cell2mat([y{2:end}])]
y_num =
1 2 3 10 13 16
4 5 6 11 14 17
7 8 9 12 15 18
Otherwise you would have to write a function like (not tested)
function num = toNumArray(a)
if iscell(a), num = cell2mat(a) ; else num = a ; end
end
and then use an expression like (not tested)
y_num = cell2mat(cellfun(@(x)toNumArray(x), y, 'UniformOutput', false)) ;

4 Comments

i have tried this code but it shows error that CAT arguements are not consistent
Then you have to show us explicitly the content of cells of your cell array, and their content content as well if you have nested cells, or build an explicit small example if your data is too large to display properly on this forum.
PS: this error with CAT means that there are dimension(s) mismatch between elements of your (nested-) cell arrays.
PS2: I'll have to leave in 5 minutes; having your data or an explicit example will help the next person to comment/answer. An alternative would be that you dig into your data structure and understand where the dimension mismatch is coming from, e.g.
>> y{1} % Display the 16x16 num array.
>> size(y{1})
ans =
16 16
>> y{2} % Display the content of element 2 of y, which is
% a 1x3 cell array in itself.
>> y{2}{1} % Display element 1 of this sub cell-array.
>> size(y{2}{1}) % .. if you don't want to count
ans =
...
and if it doesn't have 16 rows, then its dimension doesn't match the dimension of y{1}..
the size of y{2}{1} and size of y{1} both are equal 16 16
i have mailed you the code sir please help me to solve this problem

Sign in to comment.

More Answers (0)

Categories

Asked:

on 29 Jan 2013

Community Treasure Hunt

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

Start Hunting!