# How to combine animations, add title and edit limits

4 views (last 30 days)
Grigorios Chatziandreou on 14 Apr 2022
Answered: Geoff Hayes on 18 Apr 2022
Hello, how could i animate these 3 motions together as a smooth one? They seem to show up as different figures, once the one finishes animating the next one starts.
animate_pendulum(2*rd,tzd,(1.5*pi)-zd(:,1))
hold on
animate_pendulum(2*rm,tzm-tzm(1),(1.5*pi)-zm(:,1))
hold on
animate_pendulum(2*rc,tzc-tzc(1),(1.5*pi)-zc(:,1))
where
function animate_pendulum(L,t,thet)
% L = 1;
x = L*sin(thet);
y = -L*cos(thet);
figure
hold on
box on
axis square
set(gca,'XTick',[], 'YTick',[], ...
'XLim',[-1,+1]*1.1, 'YLim',[-1,+1]*1.1)
plh_bar = plot([0,x(1)],[0,y(1)],'-k','LineWidth',2); % Pendulum arm
plh_bob = plot(x(1),y(1),'.k','MarkerSize',30); % Pendulum bob
plot(0,-0.045,'^r','MarkerSize',10,'MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6]) % Pivot
drawnow
tic
while toc < t(end)
[~,idx] = min(abs(t - toc));
set(plh_bar,'XData',[0,x(idx)],'YData',[0,y(idx)])
set(plh_bob,'XData',x(idx),'YData',y(idx))
drawnow
end
set(plh_bar,'XData',[0,x(end)],'YData',[0,y(end)])
set(plh_bob,'XData',x(end),'YData',y(end))
drawnow
Also I want to add the following but dont know where:
ylimits=[-0.3,1.3];
ylim(ylimits)
xlimits=[-1.3,1.3];
xlim(xlimits)
title('ROOF MOTION REPRESENTATION','RETRACTION')

Geoff Hayes on 18 Apr 2022
@Grigorios Chatziandreou - the animate_pendulum function creates a figure each time it is called which is why you will see one figure for each pendulum. Also, because of the while loop, the subsequent call to this function will be blocked until the while loop completes. If you want one figure to show all of the pendulums, then you could modify the code so that you are passing in data for each pendulum (whether you do this as an array for each input parameter, or an array of structs where each struct encapsulates the data for the pendulum, etc.) and so that each is drawn simulataneously. Alternatively, you could create a figure and maybe a timer for each pendulum whose timer callback will update the figure with the new pendulum position.
I couldn't run your code because it wasn't clear what the inputs should be (L is length, t is an array of times in seconds presumably, and theta an angle). From the code, it looks like there is an assumption that theta is an array of angles so that x and y are arrays too (so you must be calculating the angles elsewhere).
As for the other code, why not add it in as soon as you create the axes? Or does that fail for some reason?

### Categories

Find more on General Applications in Help Center and File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by