MATLAB: Convert Cell to Matrix Let' say, I have cell_A: 5×1 cell cell_A={ [] ; [1] ; [5]; [] ; [2]}; How can I convert it to be matrix? 5-by-1 matrix result_matrix=[ []; 1; 5 ; [] ; 2 ]; Best Answer As sir Walter mentioned already , you cannot have holes in matrices . So you could replace them with nans perhaps:cell_A(cellfun('isempty',cell_A)) = {NaN};Matrix = cell2mat(cell_A) Related SolutionsMATLAB: Manipulation of a cell array- Cell_array = {1,[],magic(2);'stirng',[],[]}cellfun('isempty',Cell_array) MATLAB: Filling missing elements in a matrix. Use F = fillmissing(A,method); requires >=r2016bHere's a demo% Create a 20x10 cell array of integer values% and replace 100 of the values with emptiesA = num2cell(randi(9,20,10)); A(randi(numel(A),1,100)) = {[]}; % Fill empties with NaNsA(cellfun(@isempty,A)) = {NaN}; % Convert to matrix and replace all empties with % previous value except rows that lead with emptyF = fillmissing(cell2mat(A).','previous').'; % Replace the leftover rows the lead with emptiesF = fillmissing(F.','next').'; Alternatively you could try this but in my quick tests, it didn't work so wellF = fillmissing(cell2mat(A).','previous','EndValues','next').'; If you need to convert the matrix back to a cell array (not recommended)C = num2cell(F); Related QuestionXlswriteReplace elements of a cellI would like to replace NaN value (in 1 row and 306 columns ) instead of empty matrices inside a cell array of 6804 elementsHow to convert cell to matrixRemoving empty cell in arrayRemove cell array content matrix with condition: length of element in cell smaller than specific value
Best Answer