根据字段的值选择结构数组中的特定单元格

时间:2022-07-18 21:16:56

Data looks more like this:

数据看起来更像是这样的:

DATA = struct('Direction',{[1,1,1,1],[1,1,2,1],[2,2,2,2,2],[2,2,2,2,1,2],[2,2,2,2]},'Tr‌ial'{'correct','incorrect','incorrect','correct','correct'}); 

this is just an example and i have other fields as well

这只是一个例子,我也有其他领域

So for example, I just want to work with the cells in my struct which have the Trial as correct, so I want to select those cells and I would like to store those cells in a separate struct. I am not sure if I'm clear or not I apologize for that.

因此,例如,我只想使用我的结构中的单元格,其中试验是正确的,所以我想选择那些单元格,我想将这些单元格存储在一个单独的结构中。我不确定我是否清楚我为此道歉。

Same for if I want to select only those cells which the direction field that is a vector as what I have here with different sizes but I want to select only those vectors that all their elements are "2" only.

如果我只想选择那些作为向量的方向字段的单元格,就像我在这里使用不同大小但我只想选择那些所有元素都只是“2”的向量。

Thank you

谢谢

1 个解决方案

#1


1  

You can filter only the elements with Trial = 'Correct' like this:

您只能使用Trial ='Correct'过滤元素,如下所示:

DATA = DATA(arrayfun(@(x) strcmp(x.Trial, 'correct'), DATA))

If you also want to filter only the elements where Direction = 2 (all values), then also do this:

如果您还想仅过滤Direction = 2(所有值)的元素,那么也执行以下操作:

DATA = DATA(arrayfun(@(x) all(x.Direction == 2), DATA))

Or you can do all above in just one line like this:

或者你可以在这一行中做到这一点:

DATA = DATA(arrayfun(@(x) strcmp(x.Trial, 'correct') & all(x.Direction == 2), DATA))

#1


1  

You can filter only the elements with Trial = 'Correct' like this:

您只能使用Trial ='Correct'过滤元素,如下所示:

DATA = DATA(arrayfun(@(x) strcmp(x.Trial, 'correct'), DATA))

If you also want to filter only the elements where Direction = 2 (all values), then also do this:

如果您还想仅过滤Direction = 2(所有值)的元素,那么也执行以下操作:

DATA = DATA(arrayfun(@(x) all(x.Direction == 2), DATA))

Or you can do all above in just one line like this:

或者你可以在这一行中做到这一点:

DATA = DATA(arrayfun(@(x) strcmp(x.Trial, 'correct') & all(x.Direction == 2), DATA))