How to unpack a table with a column of structs while preserving other columns?

10 views (last 30 days)
I have a table with 2 fields, ['A', 'B'] where 'B' has structs of 1xn in each cell corresponding to A. So the table looks something like this:
A B
1 {1xn}
2 {1xm}
. .
. .
What I want is to flatten the structs while retaining the corresponding relations with the other column (The values in the other column can be repeated):
A B
1 1x1
1 1x2
1 1x3
2 1x1
2 1x2
. .
How can I acheive this?
Additional information:
The structs have all the same fields, just different number of rows
UPDATE:
I have attached a sample test file that can represent this better. What I currently have in the test_01.mat file is this:
What I am trying to get is a flattened structure after unpacking the contents of the structs in the second column like this:
Please let me know if I can provide any additional information, thanks!
  6 Comments
Parth Uday Sanghvi
Parth Uday Sanghvi on 3 Oct 2020
Updated with a small subset of the data I am working with. As you can see I need to create a super table with the contents of the struct in the cell unpacked into indiviual rows while maintaining the relation with the first column. Thanks!
Adam Danz
Adam Danz on 3 Oct 2020
Your structures are not purely numeric so you'd have to use sruct2table (or struct2cell) rather than struct2array.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 3 Oct 2020
Edited: Adam Danz on 3 Oct 2020
Tnew is the input table (attached).
gtTbl = cellfun(@struct2table, Tnew.groundtruth2DBB, 'Unif', false);
T = [repelem(Tnew(:,1), cellfun(@numel, Tnew.groundtruth2DBB),1), vertcat(gtTbl{:})];
Result
T =
16×5 table
sequenceName objid gtBb2D classname has3dbox
_____________________________________________________________________________________ _____ ________________________________________ __________________ ________
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 0 328 152 346 320 {'bed' } true
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 1 572.54 275.64 98.378 53.177 {'night_stand' } true
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 2 332.36 318.18 316.4 209.16 {'ottoman' } true
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 3 157.76 10.635 156.87 362.49 {'dresser_mirror'} true
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 4 1 1 221 529 {'dresser' } true
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 5 614.2 200.3 54.95 93.946 {'lamp' } false
{'SUNRGBD/kv2/kinect2data/000002_2014-05-26_14-23-37_260595134347_rgbf000103-resize'} 6 414.49 230.43 153.26 48.155 {'pillow' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 0 1 128 498 402 {'bed' } true
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 1 462 303 197 195 {'night_stand' } true
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 2 338.93 254.42 130.43 72.563 {'pillow' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 3 546.23 181.08 74.738 150.76 {'lamp' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 4 241.29 236.64 100.59 58.495 {'pillow' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 5 140.03 212.71 110.79 51.405 {'pillow' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 6 7.9766 38.11 75.334 130.28 {'lamp' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 7 46.973 195.87 106.35 48.746 {'night_stand' } false
{'SUNRGBD/kv2/kinect2data/000003_2014-05-26_14-24-42_260595134347_rgbf000040-resize'} 8 -1.7726 1.7726 68.244 238.41 {'dresser_mirror'} false
  5 Comments
Adam Danz
Adam Danz on 4 Oct 2020
Glad I could help. Alternatively you could replace the empty structures with default values if you want to keep those rows in the data.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!