How to truncate empty cells from a cell array?

조회 수: 21 (최근 30일)
Darrian Low
Darrian Low 2025년 11월 30일
댓글: Walter Roberson 2025년 11월 30일
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일

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일
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일
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일
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일
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 CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by