%function coefs(nbeg,nend,size);
nbeg=600;
nend=800;
size=200;
fd=fopen('SP500_1985_50.txt');	%Open input file
col=fscanf(fd,'%d',1);		%Read the number of columns
n=fscanf(fd,'%d',1);		%Read the number of rows
a=[];				%a  is a array of input data
for j=1:col		%Cycle for all columns
    b=fscanf(fd,'%e',n);	%Read a row of numbers
    a=[a;transpose(b)];		%Add a row to data array
end
str=[];				%Str is a list of the names for the columns
for i=1:col			%Cycle for columns
    b=fscanf(fd,'%s',1);	%Read the name
    str(i).name=b;		%adds the name to str
end
fclose(fd);			%Close input file
date=dlmread('SP500_1985_50.dt');	%reads file with dates.
a=transpose(a); %Transpose input data
logm=zeros(n,col);
nb=nbeg-size;
ne=nbeg;
nnn=nend-nbeg;
dates=[]; % array of dates of the current period
asp=[]; % array of average short paths
clust=[];% array of the average clustering coeficients
diam=[];% array of diameters of the graph.
degree=[];%array of degree distribution vectors
maxdegree=[]; %array of max degrees for all the steps
h = waitbar(0,'Computing...');
while (ne<800)
for j=1:col
    for i=nb:ne
        logm(i,j)=log(a(i,j))-log(a(i-1,j));
    end
end
    R=corrcoef(logm);
    dist=sqrt(2*(1-R));
    R1=prim2(dist);
    shortp=floyd(dist);
    asp=[asp,avg_short_path(shortp)];
    diam=[diam,diameter(shortp)];
    clust=[clust,Cluster_Coef(R)];
    maxd=0;degr=[];
    [maxd,degr]=degree_distr(R1);
    maxdegree=[maxdegree,maxd];
    degree=[degree;degr];
    dates=[dates,date(ne)];
    nb=nb+1;
    ne=ne+1;
    waitbar((nnn-nend+ne)/nnn);
    date(ne)
end
close(h);

plot(dates,clust);
title('Clustering');
grid on;
figure;
p=axes;
plot(dates,asp);
set(p,'XTick',19871021)
set(p,'XTickLabel',19871021)
title('Average short path');
grid on;
figure;
plot(dates,diam);
title('Diameter');
grid on;
figure;
plot(dates,maxdegree);
title('Max node degree');
grid on;