如何更新/动画 MATLAB 中的复杂图形?

How to update / animate a complicated figure in MATLAB?

我需要为一个由一连串矩形组成的复杂图形制作动画,形成一只手臂。 这是这个手臂在没有动画时的样子的示例:

为了让这个人物动起来,我编写了以下代码:

function renderFrame(fig, data)
    hold(ax, 'on'); % Need to hold so that new elements of the arm add themselves
    showMembers(fig, data); % Create patches and rotate them to the right angle to create members
    showJoints(fig, data); % Draw circles at the joints betwwen the members. Use the width of rectangle members  
    drawnow;
    hold(ax, 'off'); % Next rendering will replace this one; No need to hold
end

function rotateMember(fig, data, iMember, rotAngle)
    for iAngle = 1:rotAngle
        updateMemberAngle(data, i, 1); % Change thew data so the i-th member rotates by 1
        renderFrame(fig); % Show frame after the data was changed
    end
end

function main()
    fig = figure;
    ax = gca;
    axis(ax, 'equal');
    setAxis(data); % Set axis limits and create axis arrows with totalLength of the arm
    renderFrame(ax, data);
    rotateMember(fig, data, 3, 90); % Rotate 3rd member by 90 degrees
end

main()

但是我的动画帧根本不清晰。结果是这个数字:

我做错了什么?有没有一种方法可以通过清除框架来绘制具有多个部分的复杂图形并为其设置动画?

我研究过使用 newplotnextplot,但 MATLAB's documentation on the subject 一如既往地不完整。我还尝试创建图形对象,然后在每次迭代时设置数据,但是自 "graphics objects are deleted".

以来每次删除图形时它都会拒绝异常

在我看来,在你的方法中,你在绘制下一帧时并没有擦除情节。也许在绘制下一帧之前调用 cla() 会使它起作用?但是,如果以错误的顺序进行重绘,这可能会产生闪烁行为。

我建议看一下 animation techniques in Matlab 以了解制作动画的三种不同方法。

找到了清除轴子项的方法,除了两个轴颤动(箭头)。

这是一个可以正常工作的代码:

function renderFrame(renderAxes, data)

    % CHECK IF THERE ARE MORE GRAPHIC OBJECTS THAN ONLY THE QUIVERS
    if ~(length(renderAxes.Children) == 2)

        % DELETE EVERYTHING EXCEPT THE AXIS QUIVERS THAT WERE RENDERED AT THE BEGINNING
        % MATLAB ADDS NEW GRAPHIC OBJECTS TO THE BEGINNING OF AX.CHILDREN
        delete(renderAxes.Children(1:length(renderAxes.Children)-2))
    end

    showMembers(renderAxes, data); % Create patches and rotate them to the right angle to create members
    showJoints(renderAxes, data); % Draw circles at the joints betwwen the members. Use the width of rectangle members  
    drawnow;
end

function rotateMember(ax, data, iMember, rotAngle)
    for iAngle = 1:rotAngle
        updateMemberAngle(data, i, 1); % Change thew data so the i-th member rotates by 1
        renderFrame(ax, data); % Show frame after the data was changed
    end
end

function main()
    fig = figure;
    ax = gca;
    axis(ax, 'equal');

    % HOLD AXES AT THE BEGINNING OF THE SCRIPT
    hold(ax, 'on');

    setAxis(data); % Set axis limits and create axis arrows with totalLength of the arm
    renderFrame(ax, data);
    rotateMember(ax, data, 3, 90); % Rotate 3rd member by 90 degrees
end

main()