updating tiledlayout in a loop - "Invalid or deleted object"
36 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
VBBV
il 8 Giu 2023
Modificato: VBBV
il 8 Giu 2023
Delete the hold on commands that are present AFTER the scatter commands and place it BEFORE the scatter commands. Then add the function handles to every nexttile commands inside the for loop and call those function handles in the hold on commands inside the loop as shown below There is also a title command inside loop at the end, which may not be correct way and relates only to the last of subplots i.e. 4 in the figure
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');
hold on
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])
% delete this hold on
title('Top View', 'Interpreter', 'Latex')
nexttile(2)
p2 = plot(-xy(1),xz(1),'b');
hold on
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])
% delete this hold on
title('Side View', 'Interpreter', 'Latex')
nexttile(3)
p3 = plot(-xx(1),xz(1),'b');
hold on
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])
% delete this hold on
title('Front View', 'Interpreter', 'Latex')
nexttile(4)
p = plot3(xx(1),xy(1),xz(1),'b');
hold on
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
% delete this 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)
ax1 = nexttile(L,1) % add a function handle
hold(ax1,'on') % call the function handle in hold on
p1.XData = -xx(1:k);
p1.YData = -xy(1:k);
m1.XData = -xx(k);
m1.YData = -xy(k);
ax2 = nexttile(L,2) % add a function handle
hold(ax2,'on') % call the function handle in hold on
p2.XData = -xy(1:k);
p2.YData = xz(1:k);
m2.XData = -xy(k);
m2.YData = xz(k);
ax3 = nexttile(L,3) % add a function handle
hold(ax3,'on') % call the function handle in hold on
p3.XData = -xx(1:k);
p3.YData = xz(1:k);
m3.XData = -xx(k);
m3.YData = xz(k);
ax4 = nexttile(L,4) % add a function handle
hold(ax4,'on') % call the function handle in hold on
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
Più risposte (1)
the cyclist
il 8 Giu 2023
Modificato: the cyclist
il 8 Giu 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).
Vedere anche
Categorie
Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!