How do you plot arrays with each element being a point on the graph with the elements within the arrays updating within a for loop?

조회 수: 66 (최근 30일)
Hello, I've got a couple vectors that I need to plot.
Ex.
x=[24 35 66 75];
y=[5 15 34 45];
And the elements in the arrays are changing because it is within a for loop with kinematic equations for projectile motion. I originally had it working when x and y were just scalar values using:
plot(x,y,'rs','MarkerSize',20);
When I changed the for loop to accept multiple values via arrays and update the numbers within, I couldn't get the plot function to work.
I have something to the extent a nested for loop in the for loop, looking like:
for ii=1:length(x)
etc etc etc...
more physics..more physics..
for jj=1:length(x)
etc etc etc...
plot(x(jj),y(jj),'rs','MarkerSize',20);
end
end
When I have something to the extent of that, my graph freaks out and the square is jumping to each of the values within the arrays. How do I get it to plot each of the squares as a plot(x(1),y(1)); plot(x(2),y(2)); etc..? The arrays also aren't set lengths because the user gets to input how many blocks will be in this tower.
Thanks in advance,
Tyler

답변 (2개)

Image Analyst
Image Analyst 2014년 10월 2일
Put the plot() after the loop and plot the whole thing at one time. Or else if you want to plot one marker at a time inside the loop, like for animation purposes or something, then make sure you put "hold on" after you plot the first marker.
  댓글 수: 5
Image Analyst
Image Analyst 2014년 10월 4일
Can't you just do
for jj=1:length(x)
% etc etc etc...
plot(x(jj-2:jj),y(jj-2:jj),'rs','MarkerSize',20);
end
Each time in the loop it would most recent 3 markers and clear all the prior ones, as long as you didn't have "hold on". Of course you must adjust the first index to make sure it doesn't go less than 1.
Tyler
Tyler 2014년 10월 4일
How would you make it fit to variable amounts of times? It isn't always going to be three, because there is user input with no bounds. The only bounds are towh~=0 | 1. The input needs to be 2 or higher because I'm running on the assumption that the first block blows up in the initial explosion. (Since this is a basic building demolition simulation.)

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


Star Strider
Star Strider 2014년 10월 2일
I’m not certain that I understand what you want to do, but I get the impression you want to do Animation.
  댓글 수: 7
Tyler
Tyler 2014년 10월 4일
Alright, thank you. I know my code is clunky right now. I am hoping to clean it up once I get it working how I want it to.

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by