Create a new struct array from values of old struct array satisfying condition

21 次查看(过去 30 天)
Hi there,
I was wondering if there's a way to create a new struct array from an old struct array containing only the structs whose field values satisfy a certain condition. In my case, I have a 1 x 48 struct array (struct_out) with various fields, one of which is called 'condition' and contains a string use to identify the data stored in that particular struct (ex. struct_out(2).condition = "experiment03_2021_flag"). I would like to create a new struct array (new_struct) that contains only the structs from struct_out whose condition's have the substring 'flag' contained within them. I've been trying the following:
new_struct = struct_out(contains([struct_out.condition],"flag")')
but new_struct ends up being only a single, 1x1 struct rather than a whole array of structs as I'd like. Any ideas? I've already double checked that more than one struct indeed contains the substring "flag" in it's condition.
Thanks!

采纳的回答

Akshit Bagde
Akshit Bagde 2021-7-6
编辑:Akshit Bagde 2021-7-6
Hi!
You are creating a 'char' array while using [struct_out.condition], and hence ending up getting only one logical output instead of an index array.
Create a cell array and do the same thing. It should work -
idx = contains({struct_out.condition},'flag');
new_struct = struct_out(idx);
% Or directly
new_struct = struct_out(contains({struct_out.condition},'flag'));

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by