Issue with plot when using 2 FOR loops
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!