How to remove empty struct fields [ ] from a group a struct fields ?
59 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
vijay chandra
el 23 de Nov. de 2017
Respondida: Robert
el 12 de Jun. de 2020
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.
0 comentarios
Respuesta aceptada
Jos (10584)
el 24 de Nov. de 2017
Does this do what you want?
% create a structure with empty fields
S.A = 'x' ; S.B = [] ; S.C = 1:5 ;
fn = fieldnames(S)
tf = cellfun(@(c) isempty(S.(c)), fn)
S2 = rmfield(S, fn(tf))
0 comentarios
Más respuestas (2)
sourav malla
el 26 de Jun. de 2019
Editada: sourav malla
el 26 de Jun. de 2019
You can try like this:-
out = {t(~cellfun(@isempty,{t.places})).places};
t = cell2struct(out,{'places'},1);
3 comentarios
MSani
el 27 de Jun. de 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
el 12 de Jun. de 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
0 comentarios
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!