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?

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

Answers (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 Comments

When I added hold on, when it animated, it looked like someone took a big red sharpie and drew the path that the object was traveling. Also, I'm wanting to plot a marker for each x and y element. So 1 marker for x(1),y(1), second marker for x(2),y(2), etc...
Yes. Is that undesired? What you said you want is what it does. We just need to know if you're plotting the n'th marker, do you want all the prior n-1 markers up there also, or do you just want the n'th marker alone?
With hold on, if it looks like a big red sharpie then the markers must be so close together that the overlap, or else your markers are too large. How many points are you plotting? If you have just a few dozen it should be okay, especially if you enlarge your plot. If you have thousands, or more markers than pixel columns in your screen, then yes they will overlap and look like a solid line.
I'm aiming for each marker to be plotted for each time it goes through the loop, then clears. It has a nested for loop within it.
towh=input('How many stories would you like the tower?\n:');
fprintf('\n');
towharr=1:towh;
%some more arrays involving velocities and y positions
%y=.....
%viy=.....
dt=0.01
for ii=1:dt:15
for jj=1:length(towharr)
%kinematic equations involving time..
%jj is obviously going through each element of the arrays and updating them
y(jj)=viy(jj)*ii+0.5*g*ii^2;
x(jj)=vix(jj)*ii;
end
end
An example would be, if someone inputted 3, it would plot the three markers for each element in the arrays. Ex. plot(x(1),y(1));, plot(x(2),y(2)); plot(x(3),y(3)); And then once it updates for the next time step, it would remove those markers and replot them. I tried to explain it the best I could, in my head I know what I want, but it's hard to put on "paper".
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.
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.)

Sign in to comment.

I’m not certain that I understand what you want to do, but I get the impression you want to do Animation.

7 Comments

I do, I'm wanting to animate a tower exploding and I'm using kinematics to update the elements.
I’ve done some for my own amusement, but they were fairly simple. There are 82 MathWorks Blog posts on Animation. The Blogs are always good (IMO), I’ve watched a lot of them over the years, and I recommend them. There are probably several that bear on what you want to do. I’ll also be glad to provide what help I can.
I'm working on the code right now, it seems that it obliterates the marker each time it moves to the next one. Right now I'm trying to animate just 3 blocks falling in projectile motion, but when a marker is plotted, on the next loop through it obliterates the previous one and plots the next one. So it turns into a seizure inducing animation when ran. If you don't mind, will you take a look at my code and see where I may be going wrong at?
I’m missing ‘collision’. Please attach it and I’ll follow up in the morning.
I believe it was 'collide' you were referring to the line within the script that I commented; I've attached the function. I was messing around with it to try and make the blocks bounce when they hit the ground or boundaries, as do things do in elastic collisions.
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.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Asked:

on 2 Oct 2014

Commented:

on 4 Oct 2014

Community Treasure Hunt

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

Start Hunting!