Animate a line on matlab

2019-03-02 06:22发布

I would like to animate a self updating plot on matlab. For example a string vibrating between two ends. All the basic animate functions I have found in the documentation accomplish the same thing, mainly to animate an evolving plot. I.e the function remains the same but the number of points plotted increases (or decreases) in time. For example this script:

h = animatedline;
axis([0,4*pi,-1,1])

x = linspace(0,4*pi,1000);
y = sin(x);
for k = 1:length(x)
    addpoints(h,x(k),y(k));
    drawnow
end

traces a sine function as if an invisible hand was drawing it. What I would like to do is animate the entire function but with a varying parameter, like the phase or amplitude. I tried to modify using the following:

x = linspace(0,4*pi,1000);

;
axis([0,4*pi,-1,1])

for k = 1:10
    h = animatedline(x,sin(k*x))
    drawnow
end

This is sort of close to what I need but the functions are progressively appended, not replaced. This results in a total of 10 functions being plotted instead of an animation.

Does anyone understand what I need to do? If so how can this be accomplished?

1条回答
地球回转人心会变
2楼-- · 2019-03-02 06:28

What about this:

h = animatedline;
axis([0,4*pi,-1,1]);

x = linspace(0,4*pi,1000);

for k = 1:0.01:10
    y = sin(k*x);
    clearpoints(h);        
    addpoints(h,x,y);
    drawnow 
end
查看更多
登录 后发表回答