How to truncate empty cells from a cell array?

17 visualizzazioni (ultimi 30 giorni)
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 Commento
Sam Chak
Sam Chak il 30 Nov 2025

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?

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 30 Nov 2025
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]}

Più risposte (1)

Catalytic
Catalytic il 30 Nov 2025
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 Commenti
Walter Roberson
Walter Roberson il 30 Nov 2025
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 il 30 Nov 2025
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]}

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by