Delete rows in struct based on number of elements in field
1 次查看(过去 30 天)
显示 更早的评论
I have a 1x68 struct with 7 fields - Scouts. I want to delete rows where the number of elements under a field 'Vertices' is less than 20. How do I do this? I tried;
Scouts.Vertices(numel(Scouts.Vertices<20)) = []
but got Error using < Too many input arguments.
I would prefer not to run a loop.
Also tried this but comes back as rows = []
[rows,~] = find(numel(Scouts.Vertices)>10);
2 个评论
Stephen23
2022-8-22
"I have a 1x68 struct with 7 fields - Scouts. I want to delete rows ..."
Your structure only has one row.
采纳的回答
Rik
2022-8-22
If you mean you want to remove elements from your struct, you're fairly close already. Scouts.Vertices returns a comma separated list. You need to feed that to a function that will capture the output.
You could do this:
tmp={Scouts.Vertices};
tmp=cellfun('prodofsize',tmp);
ind = find(tmp>10);
But using a loop would be much simpler.
You could also use structfun, but that would only hide the loop and increase complexity as well.
2 个评论
Rik
2022-8-22
I may have been thinking of arrayfun. I don't believe I have used either of them recently. I don't think I have ever used arrayfun, other than in proving it's slower than a loop. But thank you for the correction.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!