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

20 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Akshit Bagde
Akshit Bagde le 6 Juil 2021
Modifié(e) : Akshit Bagde le 6 Juil 2021
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'));

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by