MATLAB: How to remove empty struct fields [ ] from a group a struct fields

i need fields which are not empty and i dont need empty fields.

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.

Best Answer

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