collapseStructure

Version 1.2.0.0 (2.54 KB) by Caradoc
Returns an n dimensional cell array of the values of a given field in a structure of depth n
301 Downloads
Updated 19 Jan 2010

View License

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 (2024). collapseStructure (https://www.mathworks.com/matlabcentral/fileexchange/26347-collapsestructure), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Multidimensional Arrays in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
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