How to truncate empty cells from a cell array?

48 ビュー (過去 30 日間)
Darrian Low
Darrian Low 2025 年 11 月 30 日 3:35
コメント済み: Walter Roberson 2025 年 11 月 30 日 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 件のコメント
Sam Chak
Sam Chak 2025 年 11 月 30 日 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?

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2025 年 11 月 30 日 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]}

その他の回答 (1 件)

Catalytic
Catalytic 2025 年 11 月 30 日 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 件のコメント
Walter Roberson
Walter Roberson 2025 年 11 月 30 日 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 2025 年 11 月 30 日 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]}

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by