How to truncate empty cells from a cell array?

69 visualizaciones (últimos 30 días)
Darrian Low
Darrian Low el 30 de Nov. de 2025 a las 3:35
Comentada: Walter Roberson el 30 de Nov. de 2025 a las 22:49
I have a cell with some data that has empty cells in the array. Please disregard how 'number_cell' is made, it's purely for example so readers have something to work with. In reality, I am receiving data that has empty rows and columns. I have no control over that.
number_cell = cell(4,4);
number_cell(1,1) = num2cell(1);
number_cell(1,2) = num2cell(2);
number_cell(1,4) = num2cell(3);
number_cell(2,1) = num2cell(7);
number_cell(2,2) = num2cell(8);
number_cell(2,4) = num2cell(9);
number_cell(4,1) = num2cell(14);
number_cell(4,2) = num2cell(15);
number_cell(4,4) = num2cell(17);
updated_cell = number_cell(~cellfun('isempty',number_cell)) ; %I've tried playing with this to no avail.
The issue I have is that the 3rd column and 3rd row have empty cell values. What I need to do is truncate this 4x4 cell of the one row and column of empty cells so the output is a 3x3 matrix.
Here's what I start with:
And here's my desired output:
What do I need to change in my code?
Thanks for reading!
  1 comentario
Sam Chak
Sam Chak el 30 de Nov. de 2025 a las 4:20

In industrial process control, the standard way MATLAB represents missing data, usually when importing from the data spreadsheet, is by automatically converting missing sensor values to NaN (Not a Number) because it allows for consistent data handling with the time stamps.

In your case, there are multiple approaches MATLAB can achieve that. But before that, do you really want to truncate those empty rows and columns, or fill the missing data with something else which you can identify later? Does the data contain seemingly random empty cells, instead of the entire row or column?

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 30 de Nov. de 2025 a las 4:10
Try this:
number_cell = cell(4,4);
number_cell(1,1) = num2cell(1);
number_cell(1,2) = num2cell(2);
number_cell(1,4) = num2cell(3);
number_cell(2,1) = num2cell(7);
number_cell(2,2) = num2cell(8);
number_cell(2,4) = num2cell(9);
number_cell(4,1) = num2cell(14);
number_cell(4,2) = num2cell(15);
number_cell(4,4) = num2cell(17)
number_cell = 4×4 cell array
{[ 1]} {[ 2]} {0×0 double} {[ 3]} {[ 7]} {[ 8]} {0×0 double} {[ 9]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 14]} {[ 15]} {0×0 double} {[ 17]}
emptyCells = cellfun('isempty',number_cell)
emptyCells = 4×4 logical array
0 0 1 0 0 0 1 0 1 1 1 1 0 0 1 0
rowAllEmpty = all(emptyCells, 2)
rowAllEmpty = 4×1 logical array
0 0 1 0
colsAllEmpty = all(emptyCells, 1)
colsAllEmpty = 1×4 logical array
0 0 1 0
% Delete the row(s) first.
number_cell(rowAllEmpty, :) = []
number_cell = 3×4 cell array
{[ 1]} {[ 2]} {0×0 double} {[ 3]} {[ 7]} {[ 8]} {0×0 double} {[ 9]} {[14]} {[15]} {0×0 double} {[17]}
% Now delete the columns(s).
number_cell(:, colsAllEmpty) = []
number_cell = 3×3 cell array
{[ 1]} {[ 2]} {[ 3]} {[ 7]} {[ 8]} {[ 9]} {[14]} {[15]} {[17]}

Más respuestas (1)

Catalytic
Catalytic el 30 de Nov. de 2025 a las 21:37
number_cell = cell(4,4);
number_cell(1,1) = num2cell(1);
number_cell(1,2) = num2cell(2);
number_cell(1,4) = num2cell(3);
number_cell(2,1) = num2cell(7);
number_cell(2,2) = num2cell(8);
number_cell(2,4) = num2cell(9);
number_cell(4,1) = num2cell(14);
number_cell(4,2) = num2cell(15);
number_cell(4,4) = num2cell(17)
number_cell = 4×4 cell array
{[ 1]} {[ 2]} {0×0 double} {[ 3]} {[ 7]} {[ 8]} {0×0 double} {[ 9]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 14]} {[ 15]} {0×0 double} {[ 17]}
I = ~cellfun('isempty',number_cell);
number_cell = number_cell(I(:,1), I(1,:))
number_cell = 3×3 cell array
{[ 1]} {[ 2]} {[ 3]} {[ 7]} {[ 8]} {[ 9]} {[14]} {[15]} {[17]}
  2 comentarios
Walter Roberson
Walter Roberson el 30 de Nov. de 2025 a las 22:14
That algorithm will not work.
number_cell = cell(4,4);
%number_cell(1,1) = num2cell(1);
number_cell(1,2) = num2cell(2);
number_cell(1,4) = num2cell(3);
number_cell(2,1) = num2cell(7);
number_cell(2,2) = num2cell(8);
number_cell(2,4) = num2cell(9);
number_cell(4,1) = num2cell(14);
number_cell(4,2) = num2cell(15);
number_cell(4,4) = num2cell(17)
number_cell = 4×4 cell array
{0×0 double} {[ 2]} {0×0 double} {[ 3]} {[ 7]} {[ 8]} {0×0 double} {[ 9]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 14]} {[ 15]} {0×0 double} {[ 17]}
I = ~cellfun('isempty',number_cell);
number_cell = number_cell(I(:,1), I(1,:))
number_cell = 2×2 cell array
{[ 8]} {[ 9]} {[15]} {[17]}
The rule is that the entire row or entire column has to be empty in order for the row or column to be deleted.
Walter Roberson
Walter Roberson el 30 de Nov. de 2025 a las 22:49
Corrected version:
number_cell = cell(4,4);
%number_cell(1,1) = num2cell(1);
number_cell(1,2) = num2cell(2);
number_cell(1,4) = num2cell(3);
number_cell(2,1) = num2cell(7);
number_cell(2,2) = num2cell(8);
number_cell(2,4) = num2cell(9);
number_cell(4,1) = num2cell(14);
number_cell(4,2) = num2cell(15);
number_cell(4,4) = num2cell(17)
number_cell = 4×4 cell array
{0×0 double} {[ 2]} {0×0 double} {[ 3]} {[ 7]} {[ 8]} {0×0 double} {[ 9]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 14]} {[ 15]} {0×0 double} {[ 17]}
I = ~cellfun('isempty',number_cell);
number_cell = number_cell(any(I,2), any(I,1))
number_cell = 3×3 cell array
{0×0 double} {[ 2]} {[ 3]} {[ 7]} {[ 8]} {[ 9]} {[ 14]} {[15]} {[17]}

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by