在MATLAB中生成可变量的动画线并同时绘制



我在上一个问题中使用MATLAB中的animatedline对象对代码进行了改进。现在,我正在创建两条动画线,它们表示不同的时间依赖性功能并同时绘制它们:

h1 = animatedline('Color' , 'r');
h2  = animatedline('Color' , 'b');
axis([0,4*pi,-1,1]);
x = linspace(0,4*pi,1000);
for k = 1:0.01:3
    y1 = sin(k*x);
    y2 = sin(k*x/2)
    clearpoints(h1);   
    clearpoints(h2)
    addpoints(h1,x,y1);
    addpoints(h2,x,y2);
    drawnow 
end

我想进一步概括。我真正需要的是生成用户指定nn动画线。然后,我想在循环中以某种方式对动画线数量敏感的循环中的每一行动画。

在上面的代码中,我正在为每个AnimatedLine声明一个单独的对象。我真的看不出如何将其扩展到可变数量的动画线?

这似乎有效:

% User input
n = 5;
colors = lines(n);
h(1 : n) = animatedline();
for n_param = 1 : n
    h(n_param) = animatedline('Color' , colors(n_param , :));
end
axis([0,4*pi,-1,1]);
x = linspace(0,4*pi,1000);
for k = 1:0.01:3
    for n_param = 1 : n
        % Do something with the parameter
        y = sin(k*x/n_param);
        h(n_param).clearpoints();       
        h(n_param).addpoints(x,y);
    end
    drawnow 
end

我使用一个数组来存储AnimatedLines对象的手柄,用单个颜色设置它们,然后将它们绘制在用于循环中。

最新更新