# Is there a function that will display all the fields of a nested structure in MATLAB?

474 views (last 30 days)
MathWorks Support Team on 21 Jan 2010
Commented: Willingo on 3 Mar 2022
I have a nested structure, containing levels of structures within it.
I would like to be able to view all the fields of this nested structure.

### Accepted Answer

MathWorks Support Team on 17 Oct 2016
There is not a built in function in MATLAB that would display the contents of a nested structure hierarchy.
You can achieve this by writing a function that recursively navigates through each field of the structure and displays its contents.
Find attached an example file, unfold.m, which displays the contents of nested structures. The help contents of the function contain more information on its usage.
##### 1 CommentShowHide None
KAE on 20 Nov 2019
This is useful for looking at multi-level XML files read in by xml2struct. I wish there were more XML utilities in Matlab to make it easy to extract data from many records.

Sign in to comment.

### More Answers (5)

Bryant on 9 Feb 2018
jsonencode will at least show everything
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Brent F on 14 Jul 2021
##### 2 CommentsShowHide 1 older comment
Brent F on 8 Sep 2021
It's very nice, but doesn't print the value of cell arrays. See line 394:
% Print cell array information, i.e. the size of the cell array.
% (The content of the cell array is not printed.)
I quickly added these lines to show something, but it probably needs to be more general:
...
listStr = [listStr; {[strIndent ' |' filler ' ' Field ' :' varStr]}];
% Add contents of 1-d cell array (needs generalization)
for j = 1 : length(Structure.(Field))
contents = Structure.(Field);
cellStruct = struct([Field '_' num2str(j)], contents(1,j));
cellContents = recFieldPrint(cellStruct, indent, depth, printValues, maxArrayLength, structname, sortfields);
listStr = [listStr; cellContents];
end
It's unbelievable that Matlab doesn't provide this by default.

Sign in to comment.

Tom Tacken on 23 Dec 2020
Yes, you can use disp() for this.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Marcelo Moraes on 30 Dec 2021
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Jessica on 7 Jan 2022
Here's what I came up with.
S.Conc.HO2.wow=[1:10];
S.Met.Temp=[1:10];
S.Time=[1:400];
names= unnest_fields(S, 'S')
names = 1×3 cell array
{'S.Conc.HO2.wow'} {'S.Met.Temp'} {'S.Time'}
function names= unnest_fields(S, parent, names)
% Function to list all the non-structure aka nested fields in a struct.
if nargin<2; parent='S'; end % structure name...
if nargin<3; names= {}; end % names of all non-structure fields.
fields=fieldnames(S);
for i=1:length(fields)
new_parent=append(parent,'.',cellstr(fields{i}));
if ~isa(S.(fields{i}),'struct')
% If this subfield isn't a structure then save "new_parent" to
% the output list of un-nested field names.
names=horzcat(names,new_parent);
else
% Otherwise recursively look into this one... with "new_parent".
names= unnest_fields(S.(fields{i}),new_parent, names);
end
end
end
##### 2 CommentsShowHide 1 older comment
Willingo on 3 Mar 2022
One could just call any index of the struct array then.
Or one could just continue to use the original function above and use arrayfun on the struct
Note that this may or may not "fail" if you have the same field name at different levels of the struct. You might want to use unique(names) or maybe store the level alongside the names.
Lots of options.
function names= unnest_fields(S, parent, names)
% Function to list all the non-structure aka nested fields in a struct.
if nargin<2; parent='S'; end % structure name...
if nargin<3; names= {}; end % names of all non-structure fields.
%% My edits
switch isscalar(S)
case 1
case 0
S = S(1);
otherwise
error("Impossible")
end
%% end my edits
fields=fieldnames(S);
for i=1:length(fields)
new_parent=append(parent,'.',cellstr(fields{i}));
if ~isa(S.(fields{i}),'struct')
% If this subfield isn't a structure then save "new_parent" to
% the output list of un-nested field names.
names=horzcat(names,new_parent);
else
% Otherwise recursively look into this one... with "new_parent".
names= unnest_fields(S.(fields{i}),new_parent, names);
end
end
end

Sign in to comment.

### Tags

No tags entered yet.

R2006b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!