Code covered by the BSD License  

Highlights from
structfind

3.0

3.0 | 1 rating Rate this file 33 Downloads (last 30 days) File Size: 1.93 KB File ID: #29808

structfind

by

 

22 Dec 2010 (Updated )

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

| Watch this File

File Information
Description

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

       index=structfind(a,field,value)

  inputs,
       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'

  outputs,
       index : The Struct index which match the search

 Example,

 a(1).name='blue';
 a(1).index=0;
 a(1).val='g';

 a(2).name='red';
 a(2).index=1;
 a(2).val=[1 0];

 a(3).name='green';
 a(3).index=2;
 a(3).val='g';

 a(4).name='black';
 a(4).index=3;
 a(4).val=[0 0 0];

 a(5).name='yellow';
 a(5).index=NaN;
 a(5).val=[0 1 1];

 a(6).name='orange';
 a(6).index=[];
 a(6).val=[1 1 0];

 a(7).name='brown';
 a(7).index=6;
 a(7).val={'12'};

 a(8).name='white';
 a(8).index=7;
 a(8).val.x=1;

 a(8).name='purple';
 a(8).index=8;
 a(8).val.child.value=2;

 index=structfind(a,'name','red');
 disp(['index : ' num2str(index)])

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

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

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

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

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

 index=structfind(a,'val',{'12'});
 disp(['index : ' num2str(index)])

 index=structfind(a,'val.child.value',2);
 disp(['index : ' num2str(index)])

Acknowledgements

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 (6)
01 Aug 2014 Philipe

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

31 Aug 2012 Jordi Riba

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

23 Dec 2010 Dirk-Jan Kroon

*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.

23 Dec 2010 Jos (10584)

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

*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);

22 Dec 2010 Jos (10584)

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)

Updates
23 Dec 2010

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

Contact us