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.
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.

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

リリース

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