필터 지우기
필터 지우기

How do i remove empty cells in a cell array?

조회 수: 35 (최근 30일)
S C
S C 2023년 8월 3일
편집: dpb 2023년 8월 3일
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
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?
S C
S C 2023년 8월 3일
편집: S C 2023년 8월 3일
I don't think i explained it well enough, sorry. I just wanted to extract all the data from the 1st and 5th column from the inside of each cell of osc_a_data. So i meant the content of the cell.
I did figured out a way to do so below.
[nrows,ncols]=cellfun(@size,osc_a_data)
for i=1:Nfile
Nsheet=nnz(nrows(i,:))
for j=1:Nsheet
xy{i,j}(:,1)=osc_a_data{i,j}(:,1);
xy{i,j}(:,2)=osc_a_data{i,j}(:,5);
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
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
  댓글 수: 1
S C
S C 2023년 8월 3일
편집: S C 2023년 8월 3일
Sorry about the description, still trying to learn the correct terms.
Yes, this is what i wanted but i did remove the following lines of the code to get what i want.
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
Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by