How to remove empty line from struct?

56 次查看(过去 30 天)
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