1

I have a Matlab struct that contains arrays. Specifically it is allocated like this:

info(27).field1 = [];
info(27).field2 = [];
info(27).field3 = [];

It's filled via a loop

% here simplified for your convenience
for i = 1:27
    info(i).field1 = rand(1,4);
    info(i).field2 = rand(1,4);
    info(i).field3 = rand(1,4);

And when filled (with my values) looks like this:

[1048576;0;0;0] [1;0;0;0]   [1;0;0;0]
[1047512;0;1064;0]  [0,99;0;0,01;0] [1;0;8;0]
[1047900;0;676;0]   [0,94;0;0,07;0] [2;0;3;0]
...

I want this to be a single (27x12) table that I can save as a table-file with the single values of the arrays being columns (using writetable(T,'myData.csv', ...') Somewhat looking like this (table headings may be neglected):

1.A     1.B 1.C     1.D     2.A     2.B 2.C 2.D     3.A 3.B 3.C 3.D
___     ___ ___     ___     ___     ___ ___ ___     ___ ___ ___ ___
1048576 0   0       0       1,00    0   0       0   1   0   0   0
1047512 0   1064    0       0,99    0   0,01    0   1   0   8   0
1047900 0   676     0       0,94    0   0,07    0   2   0   3   0

Or even better

1.A     1.B 1.C     1.D     2.A     2.B 2.C 2.D     3.A 3.B 3.C 3.D
___     ___ ___     ___     ___     ___ ___ ___     ___ ___ ___ ___
1048576 0   0       0       100%    0%  0%  0%      1   0   0   0
1047512 0   1064    0       99%     0%  1%  0%      1   0   8   0
1047900 0   676     0       94%     0%  7%  0%      2   0   3   0

What I've tried so far is:

T = table(info)    %obviously doesn't work

 info     
_____________

[1x27 struct]

And a workaround with a cell array

% create a cell array and try to concatenate the arrays in the array
C = struct2cell(info)
Cp = permute(C,[3 1 2]);
Cpx = horzcat(Cp(:,1),Cp(:,2),Cp(:,3));
T = table(Cpx)

T = 

                  Cpx                   
________________________________________

[4x1 double]  [4x1 double]  [4x1 double]
[4x1 double]  [4x1 double]  [4x1 double]
[4x1 double]  [4x1 double]  [4x1 double]
...

I thought horzcat would work, but somehow I can't wrap my head around why it doesn't. Somebody with a solution for this?

Suever
  • 64,497
  • 14
  • 82
  • 101
Honeybear
  • 2,928
  • 2
  • 28
  • 47

1 Answers1

1

Your call to horzcat simply concatenates the three columns of Cp (as cell arrays). They already are the columns so the output is the same as the input.

If you want to concatenate the contents together, you can first convert to a matrix using cell2mat and then pass this directly to array2table to create the table.

T = array2table(cell2mat(Cp));

Update

If your original vectors are 4x1 instead, you can do the following:

T = array2table(squeeze(cell2mat(struct2cell(info))).')
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thank you for your answer! I tried this in several variants, but got stuck with a little detail: – Honeybear Jul 21 '16 at 14:41
  • @Honeybear It looks like your comment got chopped off. What was the detail? – Suever Jul 21 '16 at 14:44
  • The vectors contained in the struct were vertical (4x1), therefore `cell2mat(Cp)` vertically stacked the values (27x3 struct became a 108x3 matrix). To fix that I changed the vectors contained in the struct (e.g. `[1;0;0;0]`) to horizontal ones (e.g. `[1 0 0 0]` with `v = v'`. Don't know how you would do that without a loop). Then it worked (27x12 table). Thanks a lot. P.S.: sorry, accidentally send the comment with Enter trying to do a line break ;) – Honeybear Jul 21 '16 at 14:46
  • @Honeybear In your initial post they were 1 x 4 so that's what I assumed they were in your data. I have added additional method for if they are 4 x 1. – Suever Jul 21 '16 at 14:51
  • Perfect, thank you, works like a charm. Upvoted, but still to little rep to show the vote. – Honeybear Jul 21 '16 at 15:53
  • @Honeybear You shouldn't need any rep to *see* the votes. – Suever Jul 21 '16 at 15:57
  • What I meant is, when I upvote your answer, stack overflow tells me: _Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score._ ;) – Honeybear Jul 22 '16 at 11:47
  • @Honeybear Oh interesting. I didn't realize that! – Suever Jul 22 '16 at 12:29