在Matlab中从循环输出到数组而不覆盖

时间:2014-10-13 14:42:39

标签: arrays matlab loops overwrite

我有一个脚本来获取simulink模型的输出并将此信息放在一个数组中,这样每个循环都会被写入,然后可以绘制/分析等。但是在运行脚本时,我得到以下内容:

test1

Outvs =

68.0000
68.0007
68.0430
68.0746

In an assignment  A(I) = B, the number of elements in B and I must be the same.

Error in test1 (line 19)
output(sensv0) = Outvs;

我的脚本如下:

%Vectors/containers to store values%
%input = zeros(4,4);
output = zeros(4,4);

%INITIAL VELOCITY (v0)
%do a sensitivity analysis for different input values
for sensv0 = 85:88
    %step 1: define input value to variable
    v0 = sensv0;
    %step 2: run simulation
    sim('sldemo_absbrake');
    %step 3: look at graphs at t=0,5,10,15s for Vehicle Speed
    Outvs = getdatasamples(vs, [2,6,11,14]);
    %Display Outvs (troubleshooting)
    Outvs
    %input(sensv0) = v0;
    output(sensv0) = Outvs;
end

1 个答案:

答案 0 :(得分:0)

处理此类事情的两种方法。

  1. 如果outvs的大小不变,您可以使用适当的大小初始化变量:

    var=zeros([4,4])
    for k=85:88
        var(k-84, :)=outvs;
    end
    
  2. outvs的大小不一,请使用单元格数组(带有曲线括号)。

    for k=85:88
        var{k-84}=outvs;
    end