Issue with plot when using 2 FOR loops

2 visualizaciones (últimos 30 días)
PASUNURU SAI VINEETH
PASUNURU SAI VINEETH el 28 de Feb. de 2022
Comentada: KSSV el 28 de Feb. de 2022
So, I built a program to compute the 3D trajectory of a sports ball until its second bounce. I attempt to vary two paramaters in a single run hence am using two FOR loops. The issue is that the 9 combinations (j: 1 to 3 and k: 1 to 3) take different times to reach the ground. Hence, if the first iteration has 170 timesteps (say) and second iteration has only 165, the 165th position of the ball is then connected to 166-170 positions from the first iteration.
To avoid this, I have been clearing the workspace after every external iteration ("clear all" command between two "end"s). I am unable to perform it after every internal iteration because the program forgets the current value of outer variable. As expected, I can't break the connections (3 horizontal lines in the plot) even with the current approach.
figure
for k=1:3
for j=1:3
% the main code
plot3(z,x,y)
hold on
end
clear all
end
Ideally, I would want to retain all the data in workspace, with no compromises to the plot. Using a matrix approach didn't work either since the remaining spaces in the columns are filled with zeros for the shorter iterations. Please help me with a better approach to eradicate this issue.

Respuestas (1)

KSSV
KSSV el 28 de Feb. de 2022
X = zeros([],1) ;
Y = zeros([],1) ;
Z = zeros([],1) ;
count = 0 ;
for k=1:3
for j=1:3
% the main code
% Get your x,y,z here
count = count+1;
X(count) = x ;
Y(count) = y ;
Z(count) = z ;
end
end
plot3(X,Y,Z)
  2 comentarios
PASUNURU SAI VINEETH
PASUNURU SAI VINEETH el 28 de Feb. de 2022
Thanks for the response. But, even with the suggested code, I'm still facing the issue of overlap of the curves as the matrices X, Y and Z are being over-written every iteration
KSSV
KSSV el 28 de Feb. de 2022
Show us your full code.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by