Extract ALL Values of an Arbitrary Depth Structu to an Array

To code is intended to create a generalized MATLAB function that traverses a struct of arbitrary depth and collects values without assuming
2 Downloads
Updated 5 Jan 2024

View License

To code is intended to create a generalized MATLAB function that traverses a struct of arbitrary depth and collects values without assuming the number of levels, names, or data types, you can use recursion. This function will also check for consistent data types across all values and throw an error if multiple data types are found.
In this implementation:
- `convertStructToUniformArray` is the main function that initializes the process and determines the output array type.
- `recursiveStructRead` is a helper recursive function that traverses the struct. It collects values and ensures data type consistency.
- The function supports strings (`char` in MATLAB) and numeric values (`double`). You can extend the `switch-case` block in `convertStructToUniformArray` to handle more data types if needed.
- An error is thrown if multiple data types are detected.
To use this function, call it with your struct as an argument:
% Define your struct
% ...
% Call the function
outputArray = convertStructToUniformArray(yourStruct);
```
This function will handle structs of varying depths and structures, assuming all values are of the same data type.

Cite As

Roman Voronov (2024). Extract ALL Values of an Arbitrary Depth Structu to an Array (https://www.mathworks.com/matlabcentral/fileexchange/157216-extract-all-values-of-an-arbitrary-depth-structu-to-an-array), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0