Create a new struct array from values of old struct array satisfying condition
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Karolina Szenkiel
el 6 de Jul. de 2021
Editada: Akshit Bagde
el 6 de Jul. de 2021
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 comentarios
Respuesta aceptada
Akshit Bagde
el 6 de Jul. de 2021
Editada: Akshit Bagde
el 6 de Jul. de 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'));
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!