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

cell operations

i am having a cell vector y= [16×16 double] {1×3 cell} {1×3 cell} {1×16 cell} {1×32 cell} {1×32 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.

Best 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)) ;