How to remove empty struct fields [ ] from a group a struct fields ?
Afficher commentaires plus anciens
A: [1X1 struct] B: [1X1 struct] C: [1X1 struct] D: [ ] E: [ ] F: [ ] G: [ ] ,...................like this some structure are there. I want to remove those empty fields from that froup of fields.
Réponse acceptée
Plus de réponses (2)
sourav malla
le 26 Juin 2019
Modifié(e) : sourav malla
le 26 Juin 2019
You can try like this:-
out = {t(~cellfun(@isempty,{t.places})).places};
t = cell2struct(out,{'places'},1);
3 commentaires
MSani
le 27 Juin 2019
When I load a matlab file, I am getting a struct with fields as below:
a =
struct with fields:
bt: {[1×1 struct] [] [] [] [1×1 struct] [1×1 struct]}
I would like to delete the empty fields in between. How do I do that and save the file without changing any structure to it?
I tried your solution but I got this error:
Dot indexing is not supported for variables of this type.
Stephen23
le 27 Juin 2019
a.bt(cellfun('isempty',a.bt)) = []
MSani
le 27 Juin 2019
@Stephen Thank you very much! This worked like a charm :)
How I should I then save the file? As I noticed that when I clicked on the save button, it seems to change the way the file is saved and messed up the array structures in the file.
Robert
le 12 Juin 2020
% Create struct with empty fields.
s.a = 'notEmpty';
s.b = [];
s.c = '';
s.d = 12;
fields = fieldnames(s);
sOut = rmfield(s, fields(structfun(@isempty, s)));
sOut =
struct with fields:
a: 'notEmpty'
d: 12
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!