Borrar filtros
Borrar filtros

How do i remove empty cells in a cell array?

35 visualizaciones (últimos 30 días)
S C
S C el 3 de Ag. de 2023
Editada: dpb el 3 de Ag. de 2023
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 comentarios
dpb
dpb el 3 de Ag. de 2023
Editada: dpb el 3 de Ag. de 2023
..."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 el 3 de Ag. de 2023
Editada: S C el 3 de Ag. de 2023
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

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 3 de Ag. de 2023
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 comentario
S C
S C el 3 de Ag. de 2023
Editada: S C el 3 de Ag. de 2023
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by