input:
% ~~~~~~
% switchVal  operation of the finction
% initialize=0;
% sample=1;
% results=2;
% plot = 3;
%
% gR  the output struct contaning the histogram
% (shuold be created with "gR = struct;" before initialization
%
% coords  x,y coordinates of partical centers
% first row x, second row y
%
% Lx  x size of area to calculate
% Ly  y size of area to calculate
%
% NumOfBins  number of bins in the final histogram
%
% output:
% ~~~~~~~
% gR is a histogram struct contaning:
% gR.count  total number of object counted
% gR.range  x axis range for the histogram
% gR.increment  increment of x axis
% gR.saveFileName  name of saved dat file
%
% method:
% ~~~~~~~
% the RDF is calculated by binnig all pair partical distances into
% a histogram, and normalizing each bin with it's Ideal gas number of
% particals.
% when binning the pair distances, we take into acount Periodic
% Boudary Conditions (PBC)
% finaly, to ansure that when r>infinity : RDF >1 , we
% normalize by multiplying RDF*2/(N1) where N is the number of
% particals.
% for more information http://www2.msm.ctw.utwente.nl/sluding/TEACHING/APiE_Script_v2011.pdf
% page 48  "Radial distribution function"

important

this function uses the functions "histogram", "distPBC2D"
"histogram" can be found here: http://www.cchem.berkeley.edu/chem195/histogram_8m.html#aedd379efd57ae78820ad8787bfab0cce
"distPBC2D" can be found here: http://www.mathworks.com/matlabcentral/fileexchange/46575distpbc2dm
%% example %%
example for the code above. please note that "radialDistribution2D" uses the functions "histogram" and "distPBC2D":
"histogram" can be found here: http://www.cchem.berkeley.edu/chem195/histogram_8m.html#aedd379efd57ae78820ad8787bfab0cce
"distPBC2D" can be found here: http://www.mathworks.com/matlabcentral/fileexchange/46575distpbc2dm
make sure these functions are in your path before you run this code.
coords = [rand(1,100); rand(1,100)]; % in this example we will calculate
% the RDF for a random sett of 100 coordinates. first row is x coordinates,
% second row is y coordinates.
Lx = 1; % board size
Ly = 1;
NumOfBins = 100; % How many bins you want in your RDF histogram. this is
% the RDF's resolution.
gR = struct; % create a struct, this is where our RDF
%(radial distribution function) will be at the end of this calculation.
% initialize gR struct
gR = radialDistribution2D(0,gR,coords,Lx,Ly,NumOfBins);
% Loop over pairs and determine the distribution of distances
gR = radialDistribution2D(1,gR,coords,Lx,Ly,NumOfBins);
% Normalize distance distribution histogram to get the RDF
gR = radialDistribution2D(2,gR,coords,Lx,Ly,NumOfBins);
% plot Result
gR = radialDistribution2D(3,gR,coords,Lx,Ly,NumOfBins);
your results are saved in "gR" struct. "gR.values" has the bins (x axis) of the RDF. "gR.histo" has the histogram (y axis) of the RDF.
1.1  added an example to the discription 

1.1  just changed the description 
Dustin Doty (view profile)
could this adapted for 3D coordinates?
adi (view profile)
to Cesar Garcia:
If you run this code according to my example in the other comment, you can plot the RDF with:
gR = radialDistribution2D(3,gR,coords,Lx,Ly,NumOfBins);
or, you can excess the x axis of the RDF in gR.values, and the y axis of the RDF in gR.histo
adi (view profile)
Hi, this is an example for the code above. please note that "radialDistribution2D" uses the functions "histogram" and "distPBC2D":
"histogram" can be found here: http://www.cchem.berkeley.edu/chem195/histogram_8m.html#aedd379efd57ae78820ad8787bfab0cce
"distPBC2D" can be found here: http://www.mathworks.com/matlabcentral/fileexchange/46575distpbc2dm
make sure these functions are in your path before you run this code.
coords = [rand(1,100); rand(1,100)]; % in this example we will calculate
% the RDF for a random sett of 100 coordinates. first row is x coordinates,
% second row is y coordinates.
Lx = 1; % board size
Ly = 1;
NumOfBins = 100; % How many bins you want in your RDF histogram. this is
% the RDF's resolution.
gR = struct; % create a struct, this is where our RDF
%(radial distribution function) will be at the end of this calculation.
% initialize gR struct
gR = radialDistribution2D(0,gR,coords,Lx,Ly,NumOfBins);
% Loop over pairs and determine the distribution of distances
gR = radialDistribution2D(1,gR,coords,Lx,Ly,NumOfBins);
% Normalize distance distribution histogram to get the RDF
gR = radialDistribution2D(2,gR,coords,Lx,Ly,NumOfBins);
% plot Result
gR = radialDistribution2D(3,gR,coords,Lx,Ly,NumOfBins);
your results are saved in "gR" struct. "gR.values" has the bins (x axis) of the RDF. "gR.histo" has the histogram (y axis) of the RDF.
Emil Hovad (view profile)
hey Adi
I am trying to run the script, but Matlab says that gR.values is empty and where do you fill gR.value in the code. Properly i have misunderstod something, first time a have seen the rdf function.
Cesar Garcia (view profile)
Hi, thank you for the code. I have a question related with it. How can i make to plot the RDF gotten by the code, i´m having problems to run the code. I'll appreciate all your help.
Best Regards