Search code examples
stringmatlabstruct

String compare in Struct (Matlab)


I need search a large struct and find the index of all the components with the same name.

For example: If the name is 13hy I need an array [1,5] returned

 structure(1,1).name = '13hy'
 structure(2,1).name = '64hy'
 structure(3,1).name = '37hy'
 structure(4,1).name = '07hy'
 structure(5,1).name = '13hy'

I have tried:

 strcmp(structure.name,'13hy')
 ismember(structure.name,'13hy')
 strfind(structure.name,'13hy')

and I keep getting the error 'Too many input arguments.' Please help


Solution

  • Use arrayfun to traverse the structure, using a strcmp-based anonymous function to test for the desired name:

    find(arrayfun(@(n) strcmp(structure(n).name, '13hy'), 1:numel(structure)))