Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

matlab - fix axes for animation

I'm working on a small simulation in matlab. For this purposes I want to create an animation of the simulated object (inverted pendulum). Unfortunatly the axes keep rescaling. I tried everything. There are similar questions, but I just can't get it to work. The best i got is the code below. Where I get both axes at the same time, the scaled from -5 to 5 and those scaled by matlab.

%init visualisation
visualisation = figure();
axis([-5 5 -5 5]);
xlim([-5 5]);
ylim([-5 5]);
ax_hand = axes;

for i = 1:N
    k1 = h * feval ( 'RHS', t0, x0, u );
    k2 = h * feval ( 'RHS', t0 + (h/2), x0 + (k1/2), u);
    k3 = h * feval ( 'RHS', t0 + h/2, x0 + k2/2, u);
    k4 = h * feval ( 'RHS', t0 + h, x0 + k3, u);
    x0 = x0 + ( k1 + 2*k2 + 2*k3 + k4 ) / 6;
    t0 = t0 + h;
    % model output
    wi(1:neqn,i+1) = x0';

    % model visualisation
    %plotting cart
    figure(visualisation);
    plot(x0(3), 0, 'ro', 'LineWidth', 3);
    %plotting pendulum
    l = 2;
    %plot(sin(x0(1))*l, cos(x0(1))*l, 'b*' , 'LineWidth', 2);
    % regulator

end;

result of the above code

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's one approach:

  • Do the first plot (possibly empty, doesn't matter). Get a handle to it, say h.
  • Set axis limits and include the statement axis manual fo freeze them.
  • Update plot (in a loop) via the 'XData' and 'YData' poperties of h.

Example:

h = plot(NaN, NaN, 'o'); %// empty plot
axis([0 10 0 5])
axis manual %// this line freezes the axes
for n = 1:10
    x = 1:n;
    y = sqrt(x);
    set(h, 'XData', x, 'YData', y)
    pause(.2)
end

enter image description here

Example with two plots:

h1 = plot(NaN, NaN, 'bo'); %// empty plot
hold on
h2 = plot(NaN, NaN, 'r*'); %// empty plot
axis([0 10 0 5])
axis manual %// this line freezes the axes
for n = 1:10
    x1 = 1:n;
    y1 = sqrt(x1);
    set(h1, 'XData', x1, 'YData', y1)
    x2 = 4.5;
    y2 = n/2-.5;
    set(h2, 'XData', x2, 'YData', y2)
    pause(.2)
end

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...