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!