结构到阵列的结构

时间:2014-04-04 11:00:59

标签: matlab structure

有没有一种快速/好的方法将结构结构转换为数组?

s1 = struct('v', 1);
s2 = struct('v', 2);
s = struct('s1', s1, 's2', s2);

我期待一个.v数组:

[1, 2]

原因: 我需要将一个Simulink.Parameter(结构体结构的结构等)传递给C S函数,我正在考虑通过使它成为一个双精度数组(在内存中是连续的)来序列化它,然后我重新编写它是我在C中的结构定义。

2 个答案:

答案 0 :(得分:1)

方法1:

s1 = struct('v', 10);
s2 = struct('v', 20);
s3 = struct('v', 30);
s4 = struct('v', 40);

s = struct('s1', s1, 's2', s2,'s3', s3, 's4', s4);

n1 = fieldnames(s);
for k1 = 1:numel(fieldnames(s))
    array1(k1) = getfield(getfield(s,char(n1(k1))),'v');
end

<强>输出

array1 =

    10    20    30    40

方法2:使用cell array获得相同的结果 -

cell_array1 = struct2cell(s);
for k1 = 1:numel(fieldnames(s))
    array1(k1) = cell_array1{k1,1}.v;
end

方法3:使用cell array无循环获得相同的结果 -

array1 = cellfun(@(x) x(:,1).v,struct2cell(s))';

扩展部分:这是针对我们有结构体结构的情况..(初始样式?)

%%// Let us create a struct of struct of struct as "s"
s11 = struct('v', 10);
s12 = struct('v', 20);
s13 = struct('v', 30);
s14 = struct('v', 40);

s1 = struct('s11', s11, 's12', s12,'s13', s13, 's14', s14);

s11 = struct('v', 110);
s12 = struct('v', 120);
s13 = struct('v', 130);
s14 = struct('v', 140);

s2 = struct('s11', s11, 's12', s12,'s13', s13, 's14', s14);

s = struct('s1', s1, 's2', s2);

%%// Processing
cell1 = struct2cell(s);
array1 = cellfun(@(x) x(:,:).v,struct2cell(vertcat(cell1{:})))'

<强>输出

array1 =

    10    20    30    40
   110   120   130   140

扩展第2部分:如果您想更深入一级,请使用此 -

%%// Let us create a struct of struct of struct as "s_1"
s11 = struct('v', 10);
s12 = struct('v', 20);
s13 = struct('v', 30);
s14 = struct('v', 40);

s1 = struct('s11', s11, 's12', s12,'s13', s13, 's14', s14);

s11 = struct('v', 110);
s12 = struct('v', 120);
s13 = struct('v', 130);
s14 = struct('v', 140);

s2 = struct('s11', s11, 's12', s12,'s13', s13, 's14', s14);

s_1 = struct('s1', s1, 's2', s2);


%%// Let us create a struct of struct of struct as "s_2"
s11 = struct('v', 0.1);
s12 = struct('v', 0.2);
s13 = struct('v', 0.3);
s14 = struct('v', 0.4);

s1 = struct('s11', s11, 's12', s12,'s13', s13, 's14', s14);

s11 = struct('v', 0.01);
s12 = struct('v', 0.02);
s13 = struct('v', 0.03);
s14 = struct('v', 0.04);

s2 = struct('s11', s11, 's12', s12,'s13', s13, 's14', s14);

s_2 = struct('s1', s1, 's2', s2);

%% Finally you would have s
s = struct('s_1', s_1, 's_2', s_2);

%%// Processing
cell1 = struct2cell(s);
for k2 = 1:2
    val1 = cell1{k2,1};
    t1 = struct2cell(val1);
    for k1=1:2
        array1(k1,:,k2) = cellfun(@(x) x(:,:).v,struct2cell(t1{k1,:}))';
    end
end

<强>输出

array1(:,:,1) =

    10    20    30    40
   110   120   130   140


array1(:,:,2) =

    0.1000    0.2000    0.3000    0.4000
    0.0100    0.0200    0.0300    0.0400

注意:如果您打算更深层次,只需为循环定义更多,并增加array1的维度。

答案 1 :(得分:1)

这个递归函数适用于任何深度级别,并且更紧凑:

function arr = f_struct2array(S, arr)
    if (isa(S, 'double'))
        arr = [arr S];
    else
        SNames = fieldnames(S); 
        for i = 1:numel(SNames) 
            arr = f_struct2array(S.(SNames{i}), arr);
        end
    end
end