MATLAB: How to removing data of structure field

indexingmaskingMATLABstructures

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 1×4)
Thank you experts!!!

Best 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'
Related Question