File Exchange

image thumbnail

Get structure field names in recursive manner

version 1.0.0.0 (3.04 KB) by Adam
Returns the field names of a structure, include the field names of any "sub-structures".

26 Downloads

Updated 13 Oct 2011

View License

NAMES = fieldnamesr(S) returns a cell array of strings containing the structure field names associated with s, the structure field names of any structures which are fields of s, any structures which are fields of fields of s, and so on.

NAMES = fieldnamesr(S,DEPTH) is an optional field which allows the depth of the search to be defined. Default is -1, which does not limit the search by depth. A depth of 1 will return only the field names of s (behaving like FIELDNAMES). A depth of 2 will return those field names, as well as the field names of any structure which is a field of s, and so on.

NAMES = fieldnamesr(...,'full') returns the names of fields which are structures, as well as the contents of those structures, as separate cells. This is unlike the default where a structure-tree is returned.

NAMES = fieldnamesr(...,'prefix') returns the names of the fields prefixed by the name of the input structure.

NAMES = fieldnamesr(...,'struct') returns only the names of fields which are structures.

Comments and Ratings (19)

Michael

Just perfect - saved me days of work!

Hi Adam,

For a very complex structure, I get this error.

Error in fieldnamesr (line 95)
if isstruct(eval(NAMES{i})) == 1

Expected one output from a curly brace or dot indexing expression, but there were 2 results.

Error in fieldnamesr (line 95)
if isstruct(eval(NAMES{i})) == 1

Shane Gore

Dan

Did not work for me :
It fell on a struct that was of the format :
a(1).b = ....
a(2).b = ...
because eval (a.b{1}) on line 95 gave an error.
"Expected one output from a curly brace or dot indexing expression, but there were
149 results."

z8080

Hi Adam

Upon calling
fieldnamesr(SPM, 'full')
, where SPM is a complex structure variable produced by neuroimaging software SPMa, I get the error

Expected one output from a curly brace or dot indexing expression, but there were 8 results.

Error in fieldnamesr (line 95)
if isstruct(eval(NAMES{i})) == 1

Any idea how to edit the script so that structures of any complexity can be parsed? Thanks!

z8080

Hi Adam

Upon calling
fieldnamesr(SPM, 'full')
, where SPM is a complex structure variable, I get the error

Expected one output from a curly brace or dot indexing expression, but there were 8 results.

Error in fieldnamesr (line 95)
if isstruct(eval(NAMES{i})) == 1

Any idea how to fix it? Thanks!

awesome, thanks!

Pieter

I had to change 3 lines to make it work:
68: NAMES = cellfun(@(x) strcat('S(1).',x,'(1)'),fieldnames(S),'UniformOutput',false) ;
99: ,NAMES{i}),x,'(1)'),fndtemp2,'UniformOutput',false) ;
125: NAMES = cellfun(@(x) regexprep(x,'[(1)]',''),NAMES,'UniformOutput',false) ;

Regards,
Pieter

Adam Danz

Nice work, thanks for sharing! I'll use this to flexibly access structures that have the same field and subfield names but not necessarily in the same structured order.

MrTwister

Does just what I hoped!
Thanks!

Emil

Saved me from writing script myself... :)

Chad Webb

Saved me the hassle of writing it. Worked great right out of the box. Haven't tested on arrays of structs yet, but meets my current needs. Thanks!

Chad Webb

SpinMan

this function throws the following error when i try

s=fieldnamesr(a)

where a.b.c(1,1).d.e = 1
where a.b.c(2,1).d.e = 2
where a.b.c(3,1).d.e = 3

??? Dot name reference on non-scalar structure.

Error in ==> fieldnamesr at 95
if isstruct(eval(NAMES{i})) == 1

i get the same error with R2011a and R2008a.

SpinMan

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux