Create a new struct array from values of old struct array satisfying condition
12 次查看(过去 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!
0 个评论
采纳的回答
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 个评论
更多回答(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!