function MSDandDiff(skala_czasu,skala_pix,filename1,filename2,directory2)

% Script to calculate Mean Square Displacement for different lag times 
%P. Nakielski 24-09-2014


nr_of_exp='exp_077';%<<<<---------Set it in 18 and 36 line
letter='Z';
% INSERT RIGHT VALUES
% Time scale % Distance scale [pix/um]
skala_pix=5.61;
% LOAD RIGHT TABELS
filename1=[letter,':\phejduk\fibers\',nr_of_exp,'\result_out\CENTR.mat'];%<<<<<<
filename2=[letter,':\phejduk\fibers\',nr_of_exp,'\result_out\ORIENT.mat'];%<<<<<<
directory2=[letter,':\phejduk\fibers\',nr_of_exp,'\result_out\'];%<<<<<<
% Run function
skala_czasu=132/1000;


load(filename1,'CENTR')
load(filename2,'ORIENT')

% Creation of empty tables
MSD=[];
MSD2=[];
MSD3=[];

% Loop over increasing lag time Tau
for krok=1:size(CENTR,1)-1
    % Creation of empty tables for evry single lag time
    przesuniecia=[];
    przesuniecia2=[];
    xy3=[];
    kat=[];
    for n=1:size(CENTR,1)
        if n+krok<=size(CENTR,1)
            % Calculation of incremental displacements
            przesuniecia(n)=CENTR(n+krok,1)-CENTR(n,1);
            przesuniecia2(n)=CENTR(n+krok,2)-CENTR(n,2);
            theta2 = pi*ORIENT/180;

            % Calculation of incremental angles in radians
            kat(n)=theta2(n+krok,1)-theta2(n,1);

        else
            % Change from laboratory frame to body fram 
            theta3 = pi*ORIENT(n)/180;
            % Rotation matrix
            R = [ cos(theta3)   sin(theta3)
                 -sin(theta3)   cos(theta3)];
            xy2=[przesuniecia/skala_pix; przesuniecia2/skala_pix];
            xy3 = R*xy2;
            % Calculation of squared displacements and MSD's
            %kwadraty=przesuniecia.^2;
            kwadraty=xy3(1,:).^2;
            msd=mean(kwadraty);
            %kwadraty2=przesuniecia2.^2;
            kwadraty2=xy3(2,:).^2;
            msd2=mean(kwadraty2);
            kwadraty3=kat.^2;

            %kwadraty2=xy3(2,:).^2;
            msd3=mean(kwadraty3);

            continue
         end
    end
    % Creation of MSD's values for different lag times
    MSD = vertcat(MSD,msd);
    MSD2 = vertcat(MSD2,msd2);
    MSD3 = vertcat(MSD3,msd3);

end
% Calculation of diffusion coefficient in x,y direction and anglular
% diffusion for the shortest lag time
diff_x=MSD(1)/(2*skala_czasu)
diff_y=MSD2(1)/(2*skala_czasu)
diff_tet=MSD3(1)/(2*skala_czasu)

% Save%%
filename_MSD = [directory2,'/','MSD.mat'];
filename_MSD2 = [directory2,'/','MSD2.mat'];
filename_MSD3 = [directory2,'/','MSD3.mat'];


filename_diffusion = [directory2,'/','diffusion_coeff.mat'];

save(filename_MSD,'MSD');
save(filename_MSD2,'MSD2');
save(filename_MSD3,'MSD3');


save(filename_diffusion,'diff_x', 'diff_y', 'diff_tet');