필터 지우기
필터 지우기

Issue with plot when using 2 FOR loops

조회 수: 2 (최근 30일)
PASUNURU SAI VINEETH
PASUNURU SAI VINEETH 2022년 2월 28일
댓글: KSSV 2022년 2월 28일
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.

답변 (1개)

KSSV
KSSV 2022년 2월 28일
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
PASUNURU SAI VINEETH
PASUNURU SAI VINEETH 2022년 2월 28일
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 2022년 2월 28일
Show us your full code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by