1

我有一个包含数组的 Matlab 结构。具体来说,它是这样分配的:

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

它是通过循环填充的

% 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);

当填充(用我的值)看起来像这样:

[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]
...

我希望这是一个(27x12)表,我可以将其保存为表文件,其中数组的单个值是列(使用writetable(T,'myData.csv', ...') Somewhat 看起来像这样(表标题可能被忽略):

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

甚至更好

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

到目前为止我尝试过的是:

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

 info     
_____________

[1x27 struct]

以及使用单元阵列的解决方法

% 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]
...

我认为 horzcat 会起作用,但不知何故我无法理解为什么它不起作用。有人对此有解决方案吗?

4

1 回答 1

1

您的调用只是连接(作为单元格数组)horzcat的三列。Cp它们已经是列,因此输出与输入相同。

如果要将内容连接在一起,可以先使用转换为矩阵cell2mat,然后将其直接传递给以array2table创建table.

T = array2table(cell2mat(Cp));

更新

如果您的原始向量是4x1相反的,您可以执行以下操作:

T = array2table(squeeze(cell2mat(struct2cell(info))).')
于 2016-07-19T15:21:41.783 回答