collapseStructure

Returns an n dimensional cell array of the values of a given field in a structure of depth n

You are now following this Submission

The code takes in a structure and a field name it is looking for. It will then recursively search through the structure for the given field name and add the value to a cell array of dimensions nxn1xn2... where n is the maximum number of records on the first layer of the structure, n1 is the maximum on the second etc.

eg.
bob(1).fred(1).mike(1).name='oneoneone';
bob(1).fred(1).mike(2).name='oneonetwo';
bob(1).fred(1).mike(3).name='oneonethree';
bob(1).fred(2).mike(1).name='onetwoone';
bob(1).fred(2).mike(2).name='onetwotwo';
bob(2).fred(1).mike(1).name='twooneone';
bob(2).fred(1).mike(2).name='twoonetwo';
bob(2).fred(2).mike(1).name='twotwoone';
bob(2).fred(2).mike(2).name='twotwotwo';

bob(1).george(1).fed='boo';
bob(1).george(2).fed='awk';

ret=collapseStructure(bob,'name');

ret(:,:,1) =

'oneoneone' 'onetwoone'
'twooneone' 'twotwoone'

ret(:,:,2) =

'oneonetwo' 'onetwotwo'
'twoonetwo' 'twotwotwo'

ret(:,:,3) =

'oneonethree' []
[] []

credit to Jos (10584) for help/writing padCell.m

Cite As

Caradoc (2026). collapseStructure (https://www.mathworks.com/matlabcentral/fileexchange/26347-collapsestructure), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.2.0.0

Changed padCell to use more elegant method; as per help from Jos

1.1.0.0

Added help and comments as suggested.

1.0.0.0