Code covered by the BSD License  

Highlights from

4.0 | 2 ratings Rate this file 89 Downloads (last 30 days) File Size: 1.93 KB File ID: #29808 Version: 1.1



Dirk-Jan Kroon (view profile)


22 Dec 2010 (Updated )

Search for a value, cell or string inside a Matlab struct

| Watch this File

File Information

 StructFind, Find the index of a certain string or value in a struct


       a : A Matlab struct, for example a(1).name='red', a(2).name='blue';
       field : The name of the field which is searched, for example 'name'
       value : The search value, for example 'blue'

       index : The Struct index which match the search



 a(2).val=[1 0];


 a(4).val=[0 0 0];

 a(5).val=[0 1 1];

 a(6).val=[1 1 0];




 disp(['index : ' num2str(index)])

 disp(['index : ' num2str(index)])

 index=structfind(a,'val',[0 0 0]);
 disp(['index : ' num2str(index)])

 disp(['index : ' num2str(index)])

 disp(['index : ' num2str(index)])

 disp(['index : ' num2str(index)])

 disp(['index : ' num2str(index)])

 disp(['index : ' num2str(index)])


This file inspired Struct Find.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
04 Dec 2014 Chad Greene

Chad Greene (view profile)

This function has proven quite helpful; thanks for sharing.

01 Aug 2014 Philipe

Great command!!!!! Just in case, can you search for two strings in a one liner? Like red and blue

Comment only
31 Aug 2012 Jordi Riba

Works really nice, it is exactly what I was missing. Many thanks !!!

Comment only
23 Dec 2010 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Jos (10584)
Thank you for your comments. I have uploaded a version which solves all your points 1,2 and 3. This version will probably be on-line this afternoon.

Comment only
23 Dec 2010 Jos (10584)

Jos (10584) (view profile)

ad 2) I would suggest not to throw an error, but issue a warning and return empty
ad 3) good solution. If you catch this error, and add the solution to the help this would be great.

23 Dec 2010 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Jos (10584)
1) You are right.
2) The error thrown by Matlab is very clear "not a fieldname"
3) instead of structfind(a,'x.y',3), you can use structfind(a.x,'y',3). This is logical because the tag-code is a string and is not evaluated, because it can be very complex and depend on local variables, like structfind(a,'x(2).y{''d''}{3}.z(e).d',3);

Comment only
22 Dec 2010 Jos (10584)

Jos (10584) (view profile)

Potentially useful, but ...
1) the terminology is confusing: MatLab talks about fields, not tags, of a struct.
2) the function errors when the second argument is not a field (aka tag) of the struct. Example:
clear a ; a.x = 3 ; structfind(a,'NOTx',3)
3) it cannot deal with fields within a field. This should be mentioned in the help
clear a ; a.x.y = 3 ; structfind(a,'x.y',3)

Comment only
23 Dec 2010 1.1

Added solutions for problem cases by comment : Jos (10584)

Contact us