필터 지우기
필터 지우기

delete columns of empty elements of a cell

조회 수: 10 (최근 30일)
Alberto Acri
Alberto Acri 2023년 6월 23일
편집: Mayur 2023년 7월 9일
Hello! I have a cell like this:
empty_elem = [];
A = {'5','11',empty_elem,empty_elem;'99','169','188',empty_elem;'250','258','267',empty_elem};
I want to delete the column consisting of null elements and transform the cell like this:
A = {'5','11',empty_elem;'99','169','188';'250','258','267'};
I would need code that can do this also considering that:
  • It may happen to have multiple null columns (and not just one).
  • Null columns are always found at the end (inside the cell).
I tried this way, but it deletes columns that I don't want to be deleted:
empty_elem = [];
A = {'5','11',empty_elem,empty_elem;'99','169','188',empty_elem;'250','258','267',empty_elem};
A(:, any(cellfun(@isempty, A), 1)) = [];

채택된 답변

Mayur
Mayur 2023년 6월 23일
편집: Mayur 2023년 7월 9일
Hi Alberto!
I understand that some extra columns are also getting deleted using the above command. You can instead use the following code snippet to achieve the desired functionality:
empty_elem = [];
A = {'5','11',empty_elem,empty_elem;'99','169','188',empty_elem;'250','258','267',empty_elem};
A(:, all(cellfun(@isempty, A), 1)) = [];
disp(A);
{'5' } {'11' } {0×0 double} {'99' } {'169'} {'188' } {'250'} {'258'} {'267' }
Using any deletes a column having at least one null value, whereas using all deletes those columns having all the values as null.
You can read more about all here: https://in.mathworks.com/help/matlab/ref/all.html

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by