Simultaneous Plotting of many trajectories together in one plot

I have two matrices representing the X and Y coordinates of various particles over time. For example if my dataset contains three particles each of which existed for 5 time units then the matrices that I will have is as follows:
Xcoordinates= [120,121,126,128,130; 107,106,106,105,105; 128,128,130,132,136];
Ycoordinates=[73,75,76,74,78; 76,80,82,87,85; 39,39,40,41,45];
Here, each row is representing one object and the columns are representing its position (X/Y coordinate) at any instant of time.
I want to plot the trajectories of the particles in the same graph. For this, presently I am using a for loop:
for i = 1: size(Xcoordinates,1)
plot(Xcoordinates(i,:),Ycoordinates(i,:))
hold on
end
hold off
I have attached the image that I get. I have many objects (~ 1000s which have lifetimes of 100s of units), thereby making the for loop very slow. Is there anyway to simultaneously plot all the objects without using a for loop?

 Accepted Answer

plot(Xcoordinates.',Ycoordinates.')
No loop required. The key is that the data for any one line should be down a column.

6 Comments

I see the result are multiple line plots, each one containing one column of the original matrix (Xcoordinates and Ycoordinates). Is it possible to get one single line plot containing all the data? I mean, the XData of a line plot contain multiple rows?
Xc = [Xcoordinates; nan(1,size(Xcoordinates,2))];
Yc = [Ycoordinates; nan(1,size(Ycoordinates,2))];
plot(Xc(:), Yc(:))
This code will draw each column as a separate visual line segment, but using only a single line() object. However, the color of each visual line segment will be the same: you have to go deep into undocumented properties to have a single line() object that is not a constant color.
Thanks a lot! The color is not a big deal, it will work.
I have a dataset that seems to have some similarities with @Himanish Basu's dataset, but when I tried to apply @Walter Roberson's solution to my dataset it didn't give me something meaningful, so I guess my dataset needs some pre-processing first in order to be able to apply that solution or I definitly need another code to deal with the type of data I have.
The dataset is attached in excel file. It's a set of data collected at a section of a freeway road by radar detector. It has 9 columns as follows: deviceno (the number given to each device, radar, here there is only one radar which is numbered 76), timestamp (at every second, the radar detects all vehicles in the detection area and collects their information, including each vehicle's position, speed, and attributes an id to each vehicle, which means same instant or timestamp can contain many vehicles, and a same vehicle can be detected in the next second and so on if it's still in the detection area), unixtime, ID (each vehicle is given an id by the radar; the vehicles are circularly number from 1 to 255, which means after the number reaches 255 it restarts numbering the following vehicles from 1 to 255, and so on), Length (vehicle length), YPos (the vehicle's Y-coordinate in the radar coordinate system, with radar as origin), XPos (the vehicle's X-coordinate), YSpeed (the vehicle's speed in Y-direction), and XSpeed (the vehicle's speed in X-direction).
What I want to do for now is to visualize the vehicles' trajectories in that section of road by plotting each vehicle trajectory in the same plot using X- and Y-coordinates.
I will appreciate a solution from you to my problem. Thank you!
I tried to plot only the firt 1-255 range of vehicle ID trajectories (see dataset attached 15_device76_1.xlsx) by using the fllowing code. It works perfectly, however I don't know how to plot all other successive 1-255 ranges on the same plot.
T = readtable('15_device76_1.xlsx');
N = max(T.ID)
hold on
for k = 1:N
Tk = T(T.ID == k,:);
plot(Tk.yPos, Tk.XPos)
end
hold off
xlabel('y')
ylabel('x')
Hi, can someone give an idea on this?

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!