How to search a field in a structure and extract all fields that match
Afficher commentaires plus anciens
Hopefully I'll explain clearly as I haven't worked with structures much. I have been given a large 1x1 structure of arrays with numerous fields. I want to search one field to match a specific string. I then want to extract from the structure all fields for those cases where the string matched. I would prefer to do this with out loops. For an array of structures, I've been able to use logical indexing to access and extract data using "index2 = cellfun(@(x) any(strcmp(x, test)), {diab2.preg})" and then just index into the structure, but this doesn't work for a structure of arrays.
Réponse acceptée
Plus de réponses (1)
George Abrahams
le 30 Déc 2022
Old question, but for future reference, most likely you wanted to use a table, not a structure of cell arrays, as this makes it easy.
data.Sen = { 'U1'; 'U2'; 'U1'; 'U1' };
data.Tid = { '1'; '1'; '1'; '2' };
data.Obj = { 'U1_1'; 'U2_1'; 'U1_2'; 'U1_1' };
data.X = { '10'; '5'; '3'; '1' };
data.Y = { '20'; '7'; '4'; '2' };
% Convert structure of cell arrays to table (fields become columns).
data = struct2table( data );
% Find logical indices of the matching table rows.
% If you don't care about case or additional letters, see: strncmp,
% strcmpi, strncmpi.
rows = strcmp( data.Sen, 'U1' );
% Extract these rows from the table.
data( rows, : )
% ans = 3×5 table
% Sen Tid Obj X Y
% {'U1'} {'1'} {'U1_1'} {'10'} {'20'}
% {'U1'} {'1'} {'U1_2'} {'3' } {'4' }
% {'U1'} {'2'} {'U1_1'} {'1' } {'2' }
If you didn't want to do that for some reason, you could use my fieldfun function on File Exchange / GitHub, which process the fields of one or more structures and outputs a structure with the same fields.
idx = strcmp( data.Sen, 'U1' );
fieldfun( @(x) x(idx)', data )
% ans = struct with fields:
% Sen: {'U1' 'U1' 'U1'}
% Tid: {'1' '1' '2'}
% Obj: {'U1_1' 'U1_2' 'U1_1'}
% X: {'10' '3' '1'}
% Y: {'20' '4' '2'}
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!