File Exchange

image thumbnail


version (1.42 KB) by Mathias Benedek
Conversion of nested structure to cell, providing overview and easy access to all sub-fields


Updated 04 Jan 2011

View License


Recursive function that converts a nested struct S with a total of n sub-fields into a nx2 cell array C.
The first column of C lists the full names of the sub-fields and the second column contains the respective content.
In order to display long field names set format to long.


S.a.b = 1:3;
S.a.c = 1:6 > 3;
S.d(1).e = {'ABC','DEF'};
S.d(2).f = 'text';

S =
a: [1x1 struct]
d: [1x2 struct]

C = nstruct2cell(S)
C =
'S.a.b' [1x3 double ]
'S.a.c' [1x6 logical]
'S.d(1).e' {1x2 cell }
'S.d(1).f' []
'S.d(2).e' []
'S.d(2).f' 'text'

ans =

Cite As

Mathias Benedek (2021). NSTRUCT2CELL (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Hoi Wong

I've used your nstruct2cell() for years. If I remembered correct, I should have left a 5 star years ago. Did the whole comment chain got erased?

Benjamin Knight

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

Inspired: getnestedfield

Community Treasure Hunt

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

Start Hunting!