How to remove empty line from struct?

조회 수: 55 (최근 30일)
Mira le
Mira le 2021년 8월 17일
답변: Walter Roberson 2023년 5월 16일
Hello every one
I have struct D with two fields: index
sequence [ ]
D :
1 [1 2 4 5]
[] []
3 [1 2 4 5]
4 [1 2 3 5]
I want to remove the empty line from D and become
1 [1 2 4 5]
3 [1 2 4 5]
4 [1 2 3 5]
thank you

답변 (2개)

Walter Roberson
Walter Roberson 2023년 5월 16일
mask = cellfun(@isempty, {D.index}) & cellfun(@isempty, {D.sequence});
D = D(~mask);

Yazan
Yazan 2021년 8월 17일
D.f1 = [1 2 3 4 5];
D.f2 = [];
% structure with 2 fields
D
D = struct with fields:
f1: [1 2 3 4 5] f2: []
% get fields
fields = fieldnames(D)
fields = 2×1 cell array
{'f1'} {'f2'}
% remove empty fields
D = rmfield(D, fields(structfun(@isempty, D)))
D = struct with fields:
f1: [1 2 3 4 5]
  댓글 수: 4
Yazan
Yazan 2021년 8월 17일
Provie your structure array so that I can take a look.
Tobias Wrammerfors
Tobias Wrammerfors 2023년 5월 16일
As far as I can tell, this seems to remove empty fields, not rows that are empty in all fields.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by