matlabplotanimated

Matlab: Animated Line in 3d


Let's say I have a nx3 matrix (in this example 100x3), where each column represents the x,y,z coordinate, in some discrete time sample, in 3d. I want to use the animatedLine function to track the coordinate with a maximum of 10 points at a given time. I've been using the Matlab documentation but still stuck on how to do it correctly.

Do I need to pre-define the grid or can Matlab automatically do that given the data? (For this example, the matrix is random, however, in the actual data set, the matrix is known)

 A = rand(100, 3)
 x = A(:, 1);
 y = A(:, 2);
 z = A(:, 3);

 grid on;
 h = animatedline('MaximumNumPoints', 10);

for k = 1:length(x)
addpoints(h,x(k),y(k),z(k));
drawnow
end

Solution

  • It looks like you aren't switching to a 3D view and animatedline does not alter the view for you automatically. Because of this, you will need to specify the 3D view yourself using view(3).

    A = rand(100, 3)
    x = A(:, 1);
    y = A(:, 2);
    z = A(:, 3);
    
    grid on;
    h = animatedline('MaximumNumPoints', 10);
    
    % Force a 3D view
    view(3);
    
    for k = 1:length(x)
        addpoints(h,x(k),y(k),z(k));
        drawnow
    end
    

    And the result as an animated GIF.

    enter image description here