How do i remove empty cells in a cell array?
조회 수: 18 (최근 30일)
이전 댓글 표시
Hello,
I have a cell array, see figure below, I want to remove the cell with [] (they are empty). I know this will cause to have different length but that is okay for what i want to do. I have tried different ways but it either removes the entire of the 2nd row or it removes the entire of column 6 to 10. I want to keep all the data in the exact location and just remove the cells with [].
Does anyone know how to only remove column 6,7,8,9 and 10 in only row 2?
Thank you.
댓글 수: 6
dpb
2023년 8월 3일
편집: dpb
2023년 8월 3일
..."create another cell array with only the 1st columns and 5th columns from the each cell."
What does that mean, precisely? It isn't consistent with the attempted code even without the complication introduced by using the dangerous length function (have 5 more rows of data in osc_a_data and it will return the number of rows, not columns, be precise in what dimension it is you want).
The precise description above would be simply
xy=osc_a_data(:,[1,5]);
the first and fifth columns of the original.
The ambiguity arises from do you mean the two cell array columns or the content of the cell?
채택된 답변
Image Analyst
2023년 8월 3일
Your description is not clear to me. See if this is what you are intending.
% Get size of input data
[rows, columns] = size(osc_a_data)
% Create output cell array.
xy = cell(rows, columns)
abort = false;
% Scan every cell taking the 1st and 5th column from each cell
% and putting those into a new cell array called xy.
for row = 1 : rows
for col = 1 : columns
% First see if we encountered an empty cell.
% Quit if we did.
if isempty(osc_a_data{row, col})
% "The issue i am having is that the Nsheet is always 10 due to []
% but i need it to stop at 5 for row 2 onwards"
abort = true;
break;
end
% Extract the 47x15 double matrix from this cell.
cellContents = osc_a_data{row, col};
% "create another cell array with only the 1st columns and 5th columns from the each cell"
newMatrix = cellContents(:, [1, 5]);
% Put the new matrix into new xy cell array.
xy{row, col} = newMatrix;
end
if abort
% Crop new xy cell array so that it does not include any rows
% with or below the first row where we found an empty cell.
xy = xy(1:row-1, :);
break;
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!