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

22 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Akshit Bagde
Akshit Bagde il 6 Lug 2021
Modificato: Akshit Bagde il 6 Lug 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'));

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by