MATLAB: Cell array data to double conversion

cell arraytype conversion

Hi all,
I have a cell array that I want to transform to a double array. Then I would like to fill in the double array with data from cell arrays. To solve this problem I have tried two things:
  • I have tried cell2mat, which gave a result that I couldent interpret and didnt work.
  • I tried str2double, this does not give an error, but it writes only NaN values in the double array.
The names of the variables are
  • Comp_all: cell array
  • dat: translate cell to double
  • dat2013: double array which I would like to fill in
How can I transfer both numeric and non-numeric data from cel arrays to double arrays?
The code is used to do this looks like:
dat = str2double(Com_all);
dat2013 = zeros(length(dat),5);
dat2013(1:end,1) = dat(1:end,1);

Best Answer

Assuming each cell has only one entry, try using cellfun with str2double to convert the cell array of strings to a cell array of doubles, then use cell2mat.
Air code (untested; may need adjustment):
dblComp_all = cellfun(@str2double, Comp_all);
dat = cell2mat(dblComp_all);