查找和过滤 MATLAB 元胞数组中的元素
我有一个具有如下结构的元素列表(元胞数组):
mystruct = struct('x', 'foo', 'y', 'bar', 's', struct('text', 'Pickabo'));
mylist = {mystruct <more similar struct elements here>};
现在我想过滤 mylist 以查找 s.text == 'Pickaboo' 或其他预定义字符串的所有结构。在 MATLAB 中实现这一目标的最佳方法是什么?显然这对于数组来说很容易,但是对于单元格来说最好的方法是什么?
I have a list (cell array) of elements with structs like this:
mystruct = struct('x', 'foo', 'y', 'bar', 's', struct('text', 'Pickabo'));
mylist = {mystruct <more similar struct elements here>};
Now I would like to filter mylist for all structs from which s.text == 'Pickaboo' or some other predefined string. What is the best way to achieve this in MATLAB? Obviously this is easy for arrays, but what is the best way to do this for cells?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 CELLFUN 来实现此目的。
然而,为什么要创建一个结构单元呢?如果您的结构体都具有相同的字段,则可以创建一个结构体数组。要获得点击,您可以使用 ARRAYFUN。
You can use CELLFUN for this.
However, why do you make a cell of structs? If your structs all have the same fields, you can make an array of structs. To get the hits, you'd then use ARRAYFUN.
如果元胞数组中的所有结构都具有相同的字段(
'x'
、'y'
和's'
),那么您可以将mylist
存储为结构体数组而不是元胞数组。您可以像这样转换mylist
:现在,如果您的所有字段
's'
也包含具有相同字段的结构,您可以以相同的方式将它们全部收集在一起,然后使用 STRCMP:这里,
isMatch
将是 逻辑索引向量 与mylist
长度相同,其中找到匹配项,否则为零。If all of your structures in your cell array have the same fields (
'x'
,'y'
, and's'
) then you can storemylist
as a structure array instead of a cell array. You can convertmylist
like so:Now, if all your fields
's'
also contain structures with the same fields in them, you can collect them all together in the same way, then check your field'text'
using STRCMP:Here,
isMatch
will be a logical index vector the same length asmylist
with ones where a match is found and zeroes otherwise.使用cellfun。
Use
cellfun
.