updating tiledlayout in a loop - "Invalid or deleted object"
Show older comments
Hello, I am trying to write a program that creates a GIF of a 3D trajectory (t, x, y, z coordinate data in file xxxy.mat) from 4 different perspectives. However, I keep getting the classic "Invalid or deleted object" error when I first try to alter the tile axes in the plotting loop. The code is pasted below and any help is appreciated.
clear;clc;
load('xxxy.mat')
%%
% Create file name variable
filename = 'animation_spiral_3POV.gif';
% Setting up the Plot
l = tiledlayout(2,2);
title(l,sprintf('Trajectory\nTime: %0.2f sec', t(1)),...
'Interpreter','Latex');
%%
% Plotting the first iterations
nexttile(1)
p1 = plot(-xx(1),-xy(1),'b');
m1 = scatter(-xx(1),-xy(1),'filled','b');
grid on % Adding grid lines
axis equal % Equal axis aspect ratio
xlim([-5 5])
ylim([-5 5])
hold on
title('Top View', 'Interpreter', 'Latex')
nexttile(2)
p2 = plot(-xy(1),xz(1),'b');
m2 = scatter(-xy(1),xz(1),'filled','b');
grid on % Adding grid lines
axis equal % Equal axis aspect ratio
xlim([-5 5])
ylim([-5 5])
hold on
title('Side View', 'Interpreter', 'Latex')
nexttile(3)
p3 = plot(-xx(1),xz(1),'b');
m3 = scatter(-xx(1),xz(1),'filled','b');
grid on % Adding grid lines
axis equal % Equal axis aspect ratio
xlim([-5 5])
ylim([-5 5])
hold on
title('Front View', 'Interpreter', 'Latex')
nexttile(4)
p = plot3(xx(1),xy(1),xz(1),'b');
m = scatter3(xx(1),xy(1),xz(1),'filled','b');
grid on % Adding grid lines
axis equal % Equal axis aspect ratio
xlim([-5 5])
ylim([-5 5])
zlim([-5 5])
view(-170,15); % Setting viewing angle
hold on
title('3D View', 'Interpreter', 'Latex')
%%
% Iterating through the length of the time array
for k = 1:200:length(t)
% Updating the line (p) and scatter points (m)
nexttile(l,1)
p1.XData = -xx(1:k);
p1.YData = -xy(1:k);
m1.XData = -xx(k);
m1.YData = -xy(k);
nexttile(l,2)
p2.XData = -xy(1:k);
p2.YData = xz(1:k);
m2.XData = -xy(k);
m2.YData = xz(k);
nexttile(l,3)
p3.XData = -xx(1:k);
p3.YData = xz(1:k);
m3.XData = -xx(k);
m3.YData = xz(k);
nexttile(l,4)
p.XData = xx(1:k);
p.YData = xy(1:k);
p.ZData = xz(1:k);
m.XData = xx(k);
m.YData = xy(k);
m.ZData = xz(k);
% Updating the title
title(l,sprintf('Trajectory\nTime: %0.2f sec', t(k)),...
'Interpreter','Latex');
% Delay
pause(0.1)
% Saving the figure
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf,...
'DelayTime',0.1);
else
imwrite(imind,cm,filename,'gif','WriteMode','append',...
'DelayTime',0.1);
end
end
Accepted Answer
More Answers (1)
the cyclist
on 8 Jun 2023
Edited: the cyclist
on 8 Jun 2023
Put a
hold on
call after each of your nexttile commands, to prevent the second plot of each tile from replacing the first one (and thereby "deleting" it).
1 Comment
Ian Heyman
on 8 Jun 2023
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!