Daniel@0: function N = cell2num(C) Daniel@0: % CELL2NUM Convert a 2D cell array to a 2D numeric array Daniel@0: % N = cell2num(C) Daniel@0: % If the cells contain column vectors, they must have the same number of rows in each row of C. Daniel@0: % Each column will be concatenated. Daniel@0: % Daniel@0: % Example 1: Daniel@0: % C = num2cell(rand(2,2)) Daniel@0: % [0.4565] [0.8214] Daniel@0: % [0.0185] [0.4447] Daniel@0: % N = cell2num(C) Daniel@0: % 0.4565 0.8214 Daniel@0: % 0.0185 0.4447 Daniel@0: % Daniel@0: % Example 2: Daniel@0: % C = cell(2, 3); Daniel@0: % for i=1:2 Daniel@0: % for j=1:3 Daniel@0: % C{i,j} = rand(i, 1); Daniel@0: % end Daniel@0: % end Daniel@0: % C = Daniel@0: % [ 0.8998] [ 0.8216] [ 0.6449] Daniel@0: % [2x1 double] [2x1 double] [2x1 double] Daniel@0: % C{2,1} = Daniel@0: % 0.8180 Daniel@0: % 0.6602 Daniel@0: % N=cell2num(C) Daniel@0: % 0.8998 0.8216 0.6449 Daniel@0: % 0.8180 0.3420 0.3412 Daniel@0: % 0.6602 0.2897 0.5341 Daniel@0: Daniel@0: Daniel@0: % error('use cell2mat in matlab 7') Daniel@0: Daniel@0: Daniel@0: if isempty(C) Daniel@0: N = []; Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: if any(cellfun('isempty', C)) %any(isemptycell(C)) Daniel@0: error('can''t convert cell array with empty cells to matrix') Daniel@0: end Daniel@0: Daniel@0: [nrows ncols] = size(C); Daniel@0: %N = reshape(cat(1, C{:}), [nrows ncols]); % this only works if C only contains scalars Daniel@0: r = 0; Daniel@0: for i=1:nrows Daniel@0: r = r + size(C{i,1}, 1); Daniel@0: end Daniel@0: c = 0; Daniel@0: for j=1:ncols Daniel@0: c = c + size(C{1,j}, 2); Daniel@0: end Daniel@0: N = reshape(cat(1, C{:}), [r c]); Daniel@0: