Search a cell array of structs
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a large one-dimensional cell array of structs. All the structs have the same format (i.e. same fields in the same order). I would like to search for a particular value in a struct field. E.g. say one of the fields is 'name'. I would like to be able to search the cell array and get back indices of all cells which contain a struct where the 'name' field contains 'sally'. Is there a straight-forward way to do this?
0 Kommentare
Akzeptierte Antwort
per isakson
am 24 Jun. 2014
Bearbeitet: per isakson
am 24 Jun. 2014
Try
cac{1} = struct( 'name', 'val1' );
cac{2} = struct( 'name', 'val2' );
cac{3} = struct( 'name', 'val3' );
cac{4} = struct( 'name', 'sally' );
strcmp( cellfun( @(sas) sas.name, cac, 'uni', false ), {'sally'} )
which returns
ans =
0 0 0 1
Straight-forward - maybe not.
5 Kommentare
per isakson
am 29 Jun. 2014
Yes, 'uni' is a valid shortening of 'UniformOutput'
@(sas) sas.name is the definition of a anonymous function and sas is just an argument.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!