Plotting multiple object trajectories together with their identity

Hi, I have a matfile 'a' which have the time in the first column, identity of objects from the first row of second column till the end and their X and Y coordinates.
What i want to do is to plot the trajectories of all these objects together like a movie so that i can keep track of the objects along with their identity displayed.
I am able to plot the trajectory for a single choosen object as shown below. Can anyone give an idea ?
Any help to solve this will be appreciated.
%% Loading mat file and keep time and coordinates of individual objects in a cell
load('a.mat')
final_plot_mat_missing_part = a(:,:);
Ucolumnnames_fpm = unique(final_plot_mat_missing_part(1,2:end));
finaL_plot_matrix_cell = cell(1,numel(Ucolumnnames_fpm));
for ii = 1:numel(finaL_plot_matrix_cell) %numel returns the number of elements.
idx_time_needed_f = final_plot_matrix(1,:) == Ucolumnnames_fpm(ii);
finaL_plot_matrix_cell{ii} = [final_plot_mat_missing_part(1:end,3),final_plot_mat_missing_part(1:end,idx_time_needed_f)];
end

 채택된 답변

darova
darova 2019년 5월 5일
I always use simple plot() and pause() to animate something
clc, clear, cla
load a.mat
np = 2; %(size(a,2)-1)/2; % number of particles
t = a(2:end,1);
hold on
% trajectories
for i = 2:2:np*2
plot3(t,a(2:end,i),a(2:end,i+1),'COlor',rand(1,3));
end
view(45,45)
xlabel('TIME')
ylabel('X AXIS')
zlabel('Y AXIS')
h = zeros(1,np); % particle handles
for i = 730:50:size(a,1) % frames
k = 0;
t = a(i,1); % time of current frame
for j = 2:2:np*2 % particles
x = a(i,j);
y = a(i,j+1);
k = k + 1;
h(k) = plot3(t,x,y,'or');
end
drawnow
pause(0.1)
delete(h)
end
hold off
Also read about VideoWriter() if you want to create a video

댓글 수: 8

@darova. Thanks for the answer. Here you plot the trajectory first and then make the particles run over those trajectories. Is there a way to do this simulataneously. What i mean is; to plot the trajectories and the paricles together with the tail of the trajectory disappearing over time?
Just draw whatever you want to see, wait a sec, clear it and draw again
@darova..thank you very much
Can you suggest me how to create a.mat file to use in the given programme please.
I tried in this way:
Input:
x = rand(30);
y = magic(30);
z = eye(30);
% Save the variable x,y,z into one *.mat file
save a.mat x y z
% Clear them out of the workspace
clear x y z
Output(Error):
Undefined function or variable 'a'.
Error in Myprg (line 40)
t = a(2:end,1);
Did you download this file?
123.PNG
@darova, I have a dataset that seems to have some similarities with the above question.
The dataset is attached in excel file 15_device76.xlsx. It's a set of data collected at an intersection approach of a freeway by a radar detector. It has 9 columns as follows: deviceno (the radar number, here only one radar numbered 76), timestamp (at every second, the radar detects all vehicles in the detection area and collects information including each vehicle's position, speed, and it attributes an ID to each vehicle. This means that all vehicles at the detection area at a given second of time are detected and stored for the same second of time, and a same vehicle can be detected again in the next second, if it's still in the detection area), unixtime, ID (each vehicle is given an ID by the radar; the vehicles are periodically number from 1 to 255, which means after the number reaches 255, the numbering of the next vehicles will begin again from 1 till 255, and so on. Like that, many vehicles will have same ID, but at different time period), Length (the 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).
For now, I only need to visualize the trajectories of all the vehicles together in the same plot by using only the ID, XPos and YPos columns, and if possible animate the vehicles movement by using the Length, and Speed columns. When I tried to plot all the trajectories for all the periods of ID together using the following code, I found that except the real trajectories (in road-lane direction), there were also some excessive lines in form of zigzag or diagonal to the actual trajectories (see the image just after).
T = readtable('15_device76.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')
I guess this is due to the plots of different trajectories with same IDs. So I think I still need some additional line of code to make it perfetct, but don't know how. However, when I tried to take only the first period or range 1-255 of vehicle ID (15_device76_1.xlsx) and plot their trajectories by using the fllowing code:
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')
it works perfectly as I want the trajectories to look without the extra lines (see image bellow). However, as my original file contains a very large amount (one day) of vehicle trajectory data to visualize, I still don't know how to continue plotting all other successive 1-255 ranges of ID over the time on the same plot. This is the issue I'm facing.
I would greatly appreciate a solution from you to my problem. Thank you all in advance!
Hi, can someone give an idea on this?
Please create a separate quesiton

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Vehicle Scenarios에 대해 자세히 알아보기

태그

질문:

2019년 5월 3일

댓글:

2021년 7월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by