How do I convert all data from structures & substructures in a cell into a table?

13 次查看(过去 30 天)
I have a cell vector, in which each entry is a structure. Some of the fields in a structure is also a structure. For example:
Data{1,1}.a=1;
Data{1,1}.b='b';
Data{1,1}.K.x='x';
Data{1,1}.K.y='y';
Data{1,1}.K.z='z';is
Data{2,1}.a=10;
Data{2,1}.K.x='x';
Data{2,1}.K.v='v';
I want to convert the fields and data from structure into a table. For example, I would like to see:
T =
a b x y z v
---------------------------------
1 'b' 'x' 'y' 'z' ''
10 '' '' 'x' '' '' 'v'

采纳的回答

MathWorks Support Team
Please download the attached file 'heteroStruct2table.m' and see the following example:
Data{1,1}.a=1;
Data{1,1}.b='b';
Data{1,1}.K.x='x';
Data{1,1}.K.y='y';
Data{1,1}.K.z='z';
Data{2,1}.a=10;
Data{2,1}.K.x='x';
Data{2,1}.K.v='v';
T = heteroStruct2table(Data,'K')
What the "heteroStruct2table.m" file does is to
1. add the missing fields in the original structs to make it become homogeneous
2. convert a cell array of homogeneous structs to table

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by