collapseStructure
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
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.