File Exchange

image thumbnail

structfind

version 1.1 (1.93 KB) by

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

29 Downloads

Updated

View License

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)])

Comments and Ratings (9)

Will Kinsman

Much slower than a for loop for the non-numeric case

Chad Greene

Chad Greene (view profile)

This function has proven quite helpful; thanks for sharing.

Philipe

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

Jordi Riba

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

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.

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.

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

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)

Updates

1.1

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

MATLAB Release
MATLAB 7.11 (R2010b)
Acknowledgements

Inspired: StructFind

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video