How to search a field in a structure and extract all fields that match
422 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Sindhu Priya
on 28 Mar 2017
Hi Dave,
I think, the following code snippet will help you.
s.f1= {'Sunday' 'Monday'}
s.f2= {'Sunday' 'Monday' 'Tuesday'}
s.f3= 'Tuesday'
s.f4= 'Wednesday'
s.f5= 'Thursday'
s.f6: 'Friday'
s.f7: 'Saturday'
index2 = structfun(@(x) any(strcmp(x, 'Monday')),s)
and the generated Output is
index2 =
7×1 logical array
1
1
0
0
0
0
0
Regards,
Sindhu
More Answers (1)
George Abrahams
on 30 Dec 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'}
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!