在 Matlab 中将 struct/array 个数组转换为单个数组
Transform struct/array of arrays into a single array in Matlab
我有一个包含数组的 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) table,我可以将其另存为 table-file,其中数组的单个值是列(使用 writetable(T,'myData.csv', ...')
有点像这样(table 标题可能会被忽略):
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 会工作,但不知何故我无法理解为什么它不工作。有人对此有解决方案吗?
您对 horzcat
的调用只是连接 Cp
的三列(作为元胞数组)。它们已经是列,因此输出与输入相同。
如果要将内容拼接在一起,可以先用cell2mat
转成矩阵,然后直接传给array2table
创建table
.
T = array2table(cell2mat(Cp));
更新
如果您的原始向量是 4x1
,您可以执行以下操作:
T = array2table(squeeze(cell2mat(struct2cell(info))).')
我有一个包含数组的 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) table,我可以将其另存为 table-file,其中数组的单个值是列(使用 writetable(T,'myData.csv', ...')
有点像这样(table 标题可能会被忽略):
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 会工作,但不知何故我无法理解为什么它不工作。有人对此有解决方案吗?
您对 horzcat
的调用只是连接 Cp
的三列(作为元胞数组)。它们已经是列,因此输出与输入相同。
如果要将内容拼接在一起,可以先用cell2mat
转成矩阵,然后直接传给array2table
创建table
.
T = array2table(cell2mat(Cp));
更新
如果您的原始向量是 4x1
,您可以执行以下操作:
T = array2table(squeeze(cell2mat(struct2cell(info))).')