How to removing data of structure field?

1 view (last 30 days)
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!!!

Accepted Answer

Stephen23
Stephen23 on 4 Jul 2019
Edited: Stephen23 on 4 Jul 2019
>> 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'

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!