MATLAB: How to plot particle trajectories and normalise to 0,0 origin

matrix to cell arrayplotting trajectories

Hi all,
I have data of lots of single particle trajectories that run for different lengths of time which I'd like to be able to plot them onto a 2D graph, with all the trajectory start positions normalised to 0,0 origin, so the plot looks a bit like the following:
My data is in an excel sheet with PARTICLE_ID, Time, X and Y co-ordinate (see attached testdata.xlsx file for example of 5 particle trajectories)
After importing the data as a matrix, I presume I must use mat2cell to convert the matrix to smaller cell arrays, with each cell array representing the data different particle trajectories? Currently I have the following code:
C = mat2cell(testdata, [59 56 240 56 10], [4])
C =
5×1 cell array
{ 59×4 double} % trajectory 1
{ 56×4 double} % trajectory 2
{240×4 double} % trajectory 3
{ 56×4 double} % trajectory 4
{ 10×4 double} % trajectory 5
However, if I have a file with >100s of trajectories, how would I code this so I don't have to manually specify the cell array sizes for each trajectory?
Then the next question is how do I normalise all the particle trajectory coordinates so that they begin at the origin 0,0?
I'd greatly appreciate any help you can offer.
Thank you in advance!
Amadeus

Best Answer

This will normalise everycell to begin at (0,0):
testdata = readmatrix('Amadeus Xu testdata.xlsx');
[Uid,~,ix] = unique(testdata(:,1));
tally = accumarray(ix, ones(size(ix)));
C = mat2cell(testdata, tally, [4])
figure
hold on
for k = 1:size(C,1)
plot(C{k}(:,3)-C{k}(1,3), C{k}(:,4)-C{k}(1,4))
end
grid
plot([0 0], ylim, 'k')
plot(xlim, [0 0], 'k')
hold off
xlabel('x')
ylabel('y')
Note that the arguments to mat2cell were not correct, so I added code to create the correct argument. The unique call is not absolutely necessary here since the IDs go from 1 to 5, however I included it in the event that is not the situation in other files. This also requires that the IDs be consecutive. Code to correct for that would be required if that is not the situation on other files.
.