struct2table produces different results when I use it on different struct files

24 views (last 30 days)
I am using strcut2table, I need a table of some results I obtained from an object detector, it has two columns one for bounding box paramters (1*4) and one for scores, the table should be the same. I get the tables from the image labeller app.
problem is that, although I'm using the exact same process, I get the right format for some data (bounding boxes like this [10,20,5,5] ) and a format that doesn't fit my work for other data (bounding box paramters separated like you see in the attached picture).
  9 Comments

Sign in to comment.

Answers (2)

dpb
dpb on 16 Aug 2018
I still think it would pay to get to the underlying reason for the difference in the beginning but a fixup if I understand your desire from the pictures (it's always much better to have the actual data to be able to make sure know precisely how it is stored rather than interpreting the output display that has differences from version to version (and I don't ever use the variable worksheet so am not familiar with it, anyway...) but--the following should serve to convert the table with the Boxes field as an array to the cell array (I think) of the other...
if ismatrix(T.Boxes) % is the Boxes data a 2D array?
[r,c]=size(T.Boxes); % find it's size (row,col) if so
T.Boxes=mat2cell(T.Boxes,ones(1,c),r); % convert to cell array for each row
end

Peter Perkins
Peter Perkins on 24 Aug 2018
I can't really tell what's going on here, but often problems with struct2table are cleared up by this:
>> help struct2table
struct2table Convert structure array to table.
T = struct2table(S) converts the structure array S to a table T. Each field
of S becomes a variable in T. ***When S is a scalar structure with N fields,
all of which have M rows, then T is an M-by-N table. When S is a non-scalar
structure array with M elements and N fields, then T is M-by-N.***
...
'AsArray' A logical value. Setting this to true causes struct2table
to always convert S to a table with LENGTH(S) rows, and
not treat a scalar structure specially as described above.
Default is false when S is a scalar structure, true otherwise.
  1 Comment
David Apolo
David Apolo on 30 Jun 2021
This is absolute gold. I spent a very long time struggling with struct2table producing cell arrays in some cases. I had not realised some of my structs were non-scalar, hence the 'AsArray' flag solved my issue.
Thank you very much sir!

Sign in to comment.

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!