How to remove empty struct fields [ ] from a group a struct fields ?

조회 수: 33 (최근 30일)
vijay chandra
vijay chandra 2017년 11월 23일
답변: Robert 2020년 6월 12일
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.

채택된 답변

Jos (10584)
Jos (10584) 2017년 11월 24일
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))

추가 답변 (2개)

sourav  malla
sourav malla 2019년 6월 26일
편집: sourav malla 2019년 6월 26일
You can try like this:-
out = {t(~cellfun(@isempty,{t.places})).places};
t = cell2struct(out,{'places'},1);
  댓글 수: 3
Stephen23
Stephen23 2019년 6월 27일
a.bt(cellfun('isempty',a.bt)) = []
MSani
MSani 2019년 6월 27일
@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
Robert 2020년 6월 12일
% 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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by