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

25 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

回答(2 个)

dpb
dpb 2018-8-16
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 2018-8-24
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 个评论
David Apolo
David Apolo 2021-6-30
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by