Hi Sina,
I understand you want to add line between points in the motion plot.
You can do it by plotting a line between the desired points in the for loop itself. You can refer to the below code
x1 = [p(1) next_pos(1)];
x2 = [p(4) next_pos(4)];
x3 = [p(7) next_pos(7)];
x4 = [p(10) next_pos(10)];
x5 = [p(13) next_pos(13)];
y1 = [p(2) next_pos(2)];
y2 = [p(5) next_pos(5)];
y3 = [p(8) next_pos(8)];
y4 = [p(11) next_pos(11)];
y5 = [p(14) next_pos(14)];
z1 = [p(3) next_pos(3)];
z2 = [p(6) next_pos(6)];
z3 = [p(9) next_pos(9)];
z4 = [p(12) next_pos(12)];
z5 = [p(15) next_pos(15)];
totaltime = seconds(0.3);
steptime = totaltime / length(x1);
figure;
hold on; % Add this line to enable plotting lines between points
for ii = 1:length(x1)-1
npoints = 200;
pointsx1 = linspace(x1(ii), x1(ii+1), npoints);
pointsy1 = linspace(y1(ii), y1(ii+1), npoints);
pointsz1 = linspace(z1(ii), z1(ii+1), npoints);
pointsx2 = linspace(x2(ii), x2(ii+1), npoints);
pointsy2 = linspace(y2(ii), y2(ii+1), npoints);
pointsz2 = linspace(z2(ii), z2(ii+1), npoints);
pointsx3 = linspace(x3(ii), x3(ii+1), npoints);
pointsy3 = linspace(y3(ii), y3(ii+1), npoints);
pointsz3 = linspace(z3(ii), z3(ii+1), npoints);
pointsx4 = linspace(x4(ii), x4(ii+1), npoints);
pointsy4 = linspace(y4(ii), y4(ii+1), npoints);
pointsz4 = linspace(z4(ii), z4(ii+1), npoints);
pointsx5 = linspace(x5(ii), x5(ii+1), npoints);
pointsy5 = linspace(y5(ii), y5(ii+1), npoints);
pointsz5 = linspace(z5(ii), z5(ii+1), npoints);
for jj = 1:npoints
pause(seconds(steptime) / npoints);
plot3([pointsx1(jj), pointsx1(jj)], [pointsy1(jj), pointsy1(jj)], [pointsz1(jj), pointsz1(jj)], '-*',...
[pointsx2(jj), pointsx2(jj)], [pointsy2(jj), pointsy2(jj)], [pointsz2(jj), pointsz2(jj)], '-o',...
[pointsx3(jj), pointsx3(jj)], [pointsy3(jj), pointsy3(jj)], [pointsz3(jj), pointsz3(jj)], '-o',...
[pointsx4(jj), pointsx4(jj)], [pointsy4(jj), pointsy4(jj)], [pointsz4(jj), pointsz4(jj)], '-o',...
[pointsx5(jj), pointsx5(jj)], [pointsy5(jj), pointsy5(jj)], [pointsz5(jj), pointsz5(jj)], '-o');
% Add lines between points
line([pointsx1(jj), pointsx2(jj)], [pointsy1(jj), pointsy2(jj)], [pointsz1(jj), pointsz2(jj)]);
line([pointsx2(jj), pointsx3(jj)], [pointsy2(jj), pointsy3(jj)], [pointsz2(jj), pointsz3(jj)]);
line([pointsx3(jj), pointsx4(jj)], [pointsy3(jj), pointsy4(jj)], [pointsz3(jj), pointsz4(jj)]);
line([pointsx4(jj), pointsx5(jj)], [pointsy4(jj), pointsy5(jj)], [pointsz4(jj), pointsz5(jj)]);
xlim([-5 30]);
ylim([-5 30]);
zlim([-5 30]);
grid on;
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Z Axis');
end
end
For more information, please refer to
Hope it helps!