Azzera filtri
Azzera filtri

Issue with plot when using 2 FOR loops

2 visualizzazioni (ultimi 30 giorni)
PASUNURU SAI VINEETH
PASUNURU SAI VINEETH il 28 Feb 2022
Commentato: KSSV il 28 Feb 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.

Risposte (1)

KSSV
KSSV il 28 Feb 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 Commenti
PASUNURU SAI VINEETH
PASUNURU SAI VINEETH il 28 Feb 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 il 28 Feb 2022
Show us your full code.

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by