MATLAB Answers

Fat Man
0

How to removing data of structure field?

Asked by Fat Man
on 4 Jul 2019
Latest activity Edited by Stephen Cobeldick on 4 Jul 2019
I have a structure array with 4 fields below:
st = struct('piece',{1,1,3,4},'food',{'cheerio','marshmallow','pizza','cereal'},'yes',{0,'food','no',0},'maybe',{'cereal',3,'yeet',6})
How can I delete all the rows that contains 'cereal' and get the rest?
I'm looking for general codes (since the given structure is not always 1x4)
Thank you experts!!!

  0 Comments

Sign in to comment.

Products


Release

R2018a

1 Answer

Answer by Stephen Cobeldick on 4 Jul 2019
Edited by Stephen Cobeldick on 4 Jul 2019
 Accepted Answer

>> st = struct('piece',{1,1,3,4},'food',{'cheerio','marshmallow','pizza','cereal'},'yes',{0,'food','no',0},'maybe',{'cereal',3,'yeet',6});
>> fun = @(s)any(structfun(@(m)isequal(m,'cereal'),s));
>> st(arrayfun(fun,st)) = []
st =
1x2 struct array with fields:
piece
food
yes
maybe
And checking the output:
>> st(1)
ans =
piece: 1
food: 'marshmallow'
yes: 'food'
maybe: 3
>> st(2)
ans =
piece: 3
food: 'pizza'
yes: 'no'
maybe: 'yeet'

  0 Comments

Sign in to comment.