Continuous plot in MATLAB

后端 未结 3 1179
后悔当初
后悔当初 2020-12-20 02:19

I have a loop in my code and I want to plot some variables,

In each iteration I plot a new point and I want it to be connected to the previous point.

Here is

3条回答
  •  我在风中等你
    2020-12-20 02:47

    You can use the plot handle and then update the properties 'XData' and 'YData' accordingly. Therefore you can plot the first point before the loop and generate the plot handle. After that, the plot handle is available and the plot can be adjusted within the loop.

    n  = 500;
    Fs = 1000;
    f1 = 10;
    t  = (0 : n - 1) / Fs;
    s = zeros(size(t));
    s(1) = sin(2 * pi * f1 * t(1));
    figure
    handle = plot(s(1), t(1), 'bo-'); %// the plot handle
    for i = 2 : n
        s(i) = sin(2 * pi * f1 * t(i));
        set(handle, 'XData', t(1:i), 'YData', s(1:i)) %// update the plot data
        axis([0 t(end) -1 1]);
        pause(0.1) %// small pause to see animation
    end
    

提交回复
热议问题