查找和过滤 MATLAB 元胞数组中的元素

发布于 2024-09-14 06:54:01 字数 314 浏览 7 评论 0原文

我有一个具有如下结构的元素列表(元胞数组):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痞味浪人 2024-09-21 06:54:01

您可以使用 CELLFUN 来实现此目的。

hits = cellfun(@(x)strcmp(x.s.text,'Pickabo'),mylist);
filteredList = mylist(hits);

然而,为什么要创建一个结构单元呢?如果您的结构体都具有相同的字段,则可以创建一个结构体数组。要获得点击,您可以使用 ARRAYFUN

You can use CELLFUN for this.

hits = cellfun(@(x)strcmp(x.s.text,'Pickabo'),mylist);
filteredList = mylist(hits);

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.

星星的軌跡 2024-09-21 06:54:01

如果元胞数组中的所有结构都具有相同的字段('x''y''s'),那么您可以将 mylist 存储为结构体数组而不是元胞数组。您可以像这样转换 mylist

mylist = [mylist{:}];

现在,如果您的所有字段 's' 也包含具有相同字段的结构,您可以以相同的方式将它们全部收集在一起,然后使用 STRCMP

s = [mylist.s];
isMatch = strcmp({s.text},'Pickabo');

这里,isMatch 将是 逻辑索引向量mylist 长度相同,其中找到匹配项,否则为零。

If all of your structures in your cell array have the same fields ('x', 'y', and 's') then you can store mylist as a structure array instead of a cell array. You can convert mylist like so:

mylist = [mylist{:}];

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:

s = [mylist.s];
isMatch = strcmp({s.text},'Pickabo');

Here, isMatch will be a logical index vector the same length as mylist with ones where a match is found and zeroes otherwise.

各自安好 2024-09-21 06:54:01

使用cellfun。

mystruct = struct('x', 'foo', 'y', 'bar', 's', struct('text', 'Pickabo'));
mystruct1 = struct('x', 'foo1', 'y', 'bar1', 's', struct('text', 'Pickabo'));
mystruct2 = struct('x', 'foo2', 'y', 'bar2', 's', struct('text', 'Pickabo1'));

mylist = {mystruct, mystruct1, mystruct2 };

string_of_interest = 'Pickabo'; %# define your string of interest here
mylist_index_of_interest = cellfun(@(x) strcmp(x.s.text,string_of_interest), mylist ); %# find the indices of the struct of interest
mylist_of_interest = mylist( mylist_index_of_interest ); %# create a new list containing only the the structs of interest

Use cellfun.

mystruct = struct('x', 'foo', 'y', 'bar', 's', struct('text', 'Pickabo'));
mystruct1 = struct('x', 'foo1', 'y', 'bar1', 's', struct('text', 'Pickabo'));
mystruct2 = struct('x', 'foo2', 'y', 'bar2', 's', struct('text', 'Pickabo1'));

mylist = {mystruct, mystruct1, mystruct2 };

string_of_interest = 'Pickabo'; %# define your string of interest here
mylist_index_of_interest = cellfun(@(x) strcmp(x.s.text,string_of_interest), mylist ); %# find the indices of the struct of interest
mylist_of_interest = mylist( mylist_index_of_interest ); %# create a new list containing only the the structs of interest
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文