MATLAB迭代保存到struct

时间:2011-11-23 11:26:22

标签: matlab vector struct save iteration

MATLAB迭代保存到struct 1)运行一个函数 2)每个输出[vectorA,vectorB],它们是不同大小的向量,应该保存在struct的每次迭代中

但是这个sytax不起作用:

for i=1:n
    [vectorA, vectorB] = anyfunction(i)
    myStruct(i).vectorA = {[vectorA]};
    myStruct(i).vectorB = {[vectorB]};
end

1 个答案:

答案 0 :(得分:1)

简单地:

n=5;
anyfunction=@(x) eig(magic(x));

for i=1:n
    [vectorA, vectorB] = anyfunction(i);
    myStruct(i).vectorA = vectorA;
    myStruct(i).vectorB = vectorB;
end

myStruct = 

1x5 struct array with fields:
    vectorA
    vectorB