How to remove empty line from struct?

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일

1 개 추천

mask = cellfun(@isempty, {D.index}) & cellfun(@isempty, {D.sequence});
D = D(~mask);
Yazan
Yazan 2021년 8월 17일

0 개 추천

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

Mira le
Mira le 2021년 8월 17일
I try this but I got an error:
Inputs to STRUCTFUN must be scalar structures.
Mira le
Mira le 2021년 8월 17일
I want to remove both fields
Yazan
Yazan 2021년 8월 17일
Provie your structure array so that I can take a look.
As far as I can tell, this seems to remove empty fields, not rows that are empty in all fields.

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

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2017b

태그

질문:

2021년 8월 17일

답변:

2023년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by