How do I update the plots of a subplot whitout redefining some constant plots?

8 vues (au cours des 30 derniers jours)
David Della Morte
David Della Morte le 29 Avr 2023
Hi everyone, I've created a code to use a manipulator robot. In the main script I recall the function "Simulazione" which plots two main results: in the first subplot shows the movements of the robot whitout any functional, while the second one shows the same movements but with a functional. I'd like to plot constant plots only one time but I don't know how to do so. To make the code added below easier to understand, I've divided the 2 groups of constant plots and then there is the whole function.
% CONSTANT PLOTS FOR THE FIRST SUBPLOT
plot3(p_i(1), p_i(2), p_i(3), 'k*', 'MarkerSize', 16)
text(p_i(1) + 0.05, p_i(2), p_i(3)+0.05, '$p_i$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_int(1), p_int(2), p_int(3),'k*', 'MarkerSize', 16)
text(p_int(1), p_int(2), p_int(3)+0.05, '$p_{int}$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_f(1), p_f(2), p_f(3),'k*', 'MarkerSize', 16)
text(p_f(1) + 0.05, p_f(2), p_f(3)+0.05, '$p_f$', 'Interpreter', 'latex', 'FontSize', 16)
grid on
axis equal
xlim([-lmax lmax])
ylim([-lmax lmax])
zlim([-lmax lmax]);
xlabel('x [m]','Interpreter','latex')
ylabel('y [m]','Interpreter','latex')
zlabel('z [m]','Interpreter','latex')
set(gca,'FontSize',16)
title("SENZA FUNZIONALE",'Interpreter','latex')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CONSTANT PLOTS FOR THE SECOND SUBPLOT
plot3(p_i(1), p_i(2), p_i(3), 'k*', 'MarkerSize', 16)
text(p_i(1) + 0.05, p_i(2), p_i(3)+0.05, '$p_i$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_int(1), p_int(2), p_int(3),'k*', 'MarkerSize', 16)
text(p_int(1), p_int(2), p_int(3)+0.05, '$p_{int}$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_f(1), p_f(2), p_f(3),'k*', 'MarkerSize', 16)
text(p_f(1) + 0.05, p_f(2), p_f(3)+0.05, '$p_f$', 'Interpreter', 'latex', 'FontSize', 16)
axis equal
xlim([-lmax lmax])
ylim([-lmax lmax])
zlim([-lmax lmax])
xlabel('x [m]','Interpreter','latex')
ylabel('y [m]','Interpreter','latex')
ylabel('z [m]','Interpreter','latex')
set(gca,'FontSize',16)
grid on
title("CON FUNZIONALE",'Interpreter','latex')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION I USE
function Simulazione(XY1, XY2, XY3, XYe, XYd, XY1_man, XY2_man, XY3_man, XYe_man, t, p_i, p_int, p_f, W, W_fun, lmax)
close all
figure()
for i = 1:10:length(t)
sgt = sgtitle('4 DoF Manipulator','Color','black');
sgt.FontSize = 20;
sgt.FontWeight = 'bold';
sgt.Interpreter = 'latex';
subplot(1, 2, 1)
plot3(0, 0, 0, '.k', 'MarkerSize', 20)
hold on
plot3([0 XY1(i,1)], [0 XY1(i,2)], [0, XY1(i,3)], '-r', 'Linewidth', 4)
plot3(XY1(i,1), XY1(i,2), XY1(i,3), '.k', 'MarkerSize', 20)
plot3([XY1(i,1) XY2(i,1)], [XY1(i,2) XY2(i,2)], [XY1(i,3) XY2(i,3)], '-b', 'Linewidth', 4)
plot3(XY2(i,1), XY2(i,2), XY2(i,3), '.k', 'MarkerSize', 20)
plot3([XY2(i,1) XY3(i,1)], [XY2(i,2) XY3(i,2)+0.095], [XY2(i,3) XY3(i,3)],'-g', 'Linewidth', 4)
plot3(XY3(i,1), XY3(i,2)+0.095, XY3(i,3), '.k', 'MarkerSize', 20)
plot3([XY3(i,1) XYe(i,1)], [XY3(i,2)+0.095 XYe(i,2)], [XY3(i,3) XYe(i,3)], '-m', 'Linewidth', 4)
plot3(XYd(1:i, 1), XYd(1:i, 2), XYd(1:i, 3), '--r', 'LineWidth', 3)
plot3(XYe(1:i, 1), XYe(1:i, 2), XYe(1:i, 3), '-b', 'LineWidth', 1.5)
plot3(p_i(1), p_i(2), p_i(3), 'k*', 'MarkerSize', 16)
text(p_i(1) + 0.05, p_i(2), p_i(3)+0.05, '$p_i$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_int(1), p_int(2), p_int(3),'k*', 'MarkerSize', 16)
text(p_int(1), p_int(2), p_int(3)+0.05, '$p_{int}$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_f(1), p_f(2), p_f(3),'k*', 'MarkerSize', 16)
text(p_f(1) + 0.05, p_f(2), p_f(3)+0.05, '$p_f$', 'Interpreter', 'latex', 'FontSize', 16)
grid on
axis equal
xlim([-lmax lmax])
ylim([-lmax lmax])
zlim([-lmax lmax]);
xlabel('x [m]','Interpreter','latex')
ylabel('y [m]','Interpreter','latex')
zlabel('z [m]','Interpreter','latex')
set(gca,'FontSize',16)
title("SENZA FUNZIONALE",'Interpreter','latex')
hold off
subplot(1, 2, 2)
plot3(0, 0, 0,'.k','MarkerSize',20)
hold on
plot3([0 XY1_man(i,1)], [0 XY1_man(i,2)], [0 XY1_man(i,3)],'-r','Linewidth',4)
plot3(XY1_man(i,1), XY1_man(i,2), XY1_man(i,3),'.k','MarkerSize',20)
plot3([XY1_man(i,1) XY2_man(i,1)], [XY1_man(i,2) XY2_man(i,2)], [XY1_man(i,3) XY2_man(i,3)],'-b','Linewidth',4)
plot3(XY2_man(i,1), XY2_man(i,2), XY2_man(i,3),'.k','MarkerSize',20)
plot3([XY2_man(i,1) XY3_man(i,1)], [XY2_man(i,2) XY3_man(i,2)+0.095], [XY2_man(i,3) XY3_man(i,3)],'-g','Linewidth',4)
plot3(XY3(i,1), XY3(i,2)+0.095, XY3(i,3),'.k', 'MarkerSize', 20)
plot3([XY3(i,1) XYe(i,1)], [XY3(i,2)+0.095 XYe(i,2)], [XY3(i,3) XYe(i,3)],'-m', 'Linewidth', 4)
plot3(XYd(1:i,1), XYd(1:i,2), XYd(1:i,3),'--r','LineWidth',3)
plot3(XYe_man(1:i,1), XYe_man(1:i,2), XYe_man(1:i,3),'-b','LineWidth',1.5)
plot3(p_i(1), p_i(2), p_i(3), 'k*', 'MarkerSize', 16)
text(p_i(1) + 0.05, p_i(2), p_i(3)+0.05, '$p_i$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_int(1), p_int(2), p_int(3),'k*', 'MarkerSize', 16)
text(p_int(1), p_int(2), p_int(3)+0.05, '$p_{int}$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_f(1), p_f(2), p_f(3),'k*', 'MarkerSize', 16)
text(p_f(1) + 0.05, p_f(2), p_f(3)+0.05, '$p_f$', 'Interpreter', 'latex', 'FontSize', 16)
axis equal
xlim([-lmax lmax])
ylim([-lmax lmax])
zlim([-lmax lmax])
xlabel('x [m]','Interpreter','latex')
ylabel('y [m]','Interpreter','latex')
ylabel('z [m]','Interpreter','latex')
set(gca,'FontSize',16)
grid on
title("CON FUNZIONALE",'Interpreter','latex')
hold off
pause(0.05)
end
end

Réponses (1)

Divyanshu
Divyanshu le 19 Mai 2023
Hi David,
As per the description provided, I have following understanding of the problem:
  • Function “Simulazione” is called from a main script and this function has the code to plot two subplots.
  • There are two constant plots which you want to plot only once and as the code is present inside a for loop, you are facing the challenge to plot it once.
So, according to above understanding one possible workaround is to place the constant plots code inside ‘if’ block as shown in the below code snippet:
if(j == 1)
plot3(p_i(1), p_i(2), p_i(3), 'k*', 'MarkerSize', 16)
text(p_i(1) + 0.05, p_i(2), p_i(3)+0.05, '$p_i$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_int(1), p_int(2), p_int(3),'k*', 'MarkerSize', 16)
text(p_int(1), p_int(2), p_int(3)+0.05, '$p_{int}$', 'Interpreter', 'latex', 'FontSize', 16)
plot3(p_f(1), p_f(2), p_f(3),'k*', 'MarkerSize', 16)
text(p_f(1) + 0.05, p_f(2), p_f(3)+0.05, '$p_f$', 'Interpreter', 'latex', 'FontSize', 16)
grid on
axis equal
xlim([-lmax lmax])
ylim([-lmax lmax])
zlim([-lmax lmax]);
xlabel('x [m]','Interpreter','latex')
ylabel('y [m]','Interpreter','latex')
zlabel('z [m]','Interpreter','latex')
set(gca,'FontSize',16)
title("SENZA FUNZIONALE",'Interpreter','latex')
end
Similarly, do the same for the other constant plot as well.
Few pointers to take care of:
  • Declare a variable ‘j’ at the start of the function body with value of j = 1
  • And make sure to increment the value of j by 1 in the end of the loop body.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by