MATLAB: Forward, backward and central differences

backward and central differencesfishforwardtracking

Hi, I am a biology student and new to MATLAB. I am working on a project to study the behavior of small aquatic fishes. I am have captured a video of 2000 frames and the time difference between frames is 1/500 sec. I have a software which gives me the positions of the fishes. I have a MAT file which contains the data of the centroid positions of the fishes. The structure array looks like below.
Fish(1).frames=[10, 11,12,13,14];
Fish(1).position1=[20.5, 20.9, 21.3, 21.7,22.1];
Fish(1).position2=[4.1, 4.27, 4.53, 4.79,4.88];
Fish(2).frames=[1,2,3,4,5,6,7,8,9];
Fish(2).position1=[10.4,10.6,10.7,10.8,11.1, 11.47,11.82,12.31,12.44];
Fish(2).position2=[4.2, 4.7, 4.8, 4.9,5.2,5.28,5.63,5.89,6.01];
The size of the structure is 30,000 X 1. I want to calculate the velocity of the fishes based on position1 (velocity1) and position2(velocity2). I want to apply forward and backward differences to the end data points and central difference to the inside data. I don't know how to do this. Please, help me.

Best Answer

dt=1/500;
for k=1:length(Fish)
Fish(k).velocity1 = gradient(Fish(k).position1,dt);
Fish(k).velocity2 = gradient(Fish(k).position2,dt);
end