Convert nested struct-array to numeric array - MATLAB Cody - MATLAB Central

Problem 42859. Convert nested struct-array to numeric array

Difficulty:Rate

Sometimes data happens to be nicely structured in a struct-array, distributed over various levels of the struct, according to the hierarchy in the logic of the data acquisition. For fitting the data, the relevant elements of the struct-array need to be converted to a simple, multi-dimensional numeric array.

That will be your assignment.

Create a function struct2mat that takes a struct-array as an input, and returns an array, where dimensions of the output data correspond to the struc-array levels in incremental order. The first output level enters dimension 1 of the output, the 2nd dimension 2, etc.

datastruct(1).level2(1).level3(1).data = 1:4;
...
datastruct(4).level2(3).level3(2).data = 4321:4324;
dataarray = struct2mat(datastruct)

should result in

dataarray(1,1,1,1) = 1;
...
dataarray(1,1,1,4) = 4;
...
dataarray(4,3,2,3) = 4323;

I encourage you to write a function that does not use loops to access the different struct-array levels.

Solution Stats

58.82% Correct | 41.18% Incorrect
Last Solution submitted on Sep 24, 2022

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
4

Problem Recent Solvers6

Suggested Problems

More from this Author31

Community Treasure Hunt

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

Start Hunting!