How to remove empty line from struct?

18 visualizzazioni (ultimi 30 giorni)
Mira le
Mira le il 17 Ago 2021
Risposto: Walter Roberson il 16 Mag 2023
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

Risposte (2)

Walter Roberson
Walter Roberson il 16 Mag 2023
mask = cellfun(@isempty, {D.index}) & cellfun(@isempty, {D.sequence});
D = D(~mask);

Yazan
Yazan il 17 Ago 2021
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 Commenti
Yazan
Yazan il 17 Ago 2021
Provie your structure array so that I can take a look.
Tobias Wrammerfors
Tobias Wrammerfors il 16 Mag 2023
As far as I can tell, this seems to remove empty fields, not rows that are empty in all fields.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by