function democoef(nbeg,nend,size,step,filename,thr, peremesh);
%nbeg=1000;
%nend=1100;
%size=90;
fd=fopen(filename);%Open input file
filedt=strrep(filename,'.txt','.dt');
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
a=transpose(a);%Transpose input data
global peremesh;
if isempty(peremesh)
    peremesh=0;
end
if (peremesh==1)
    a=peremesh_matrix(a);
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(filedt);	%reads file with dates.
logm=zeros(nend-nbeg+1,col);
%nb=nbeg-size;
nb=nbeg;
%ne=nbeg;
ne=nbeg+size;
%nnn=nend
nnn=nend-nbeg;

%nend=nend+size;
%dates=[]; % array of dates of the current period
asp=[]; % array of average short paths
clust=[];% array of the average clustering coeficients
clust2=[];% array of the average clustering coeficients of a single node
diam=[];% array of diameters of the graph.
degree=[];%array of degree distribution vectors
degree_b=[];% Coefficient of power law degree distribution P(x)=a*k^-b; degree_k=b
maxdegree=[]; %array of max degrees for all the steps
mascorrmax=[];
mascorrmin=[];
h = waitbar(0,'Computing...');

fd=fopen('coefs.xls','w');
fprintf(fd,'Results of clustering coefficients: \t\t\tSeries:\t%s\tnbeg=\t%d\tnend=\t%d\twindow=\t%d\tstep=\t%d\t threshold=\t%d\tperemesh=\t%d\t\n',filename,nbeg,nend,size,step,thr,peremesh);
fprintf(fd,'timebeg \ttimeend\t Clustering \tAvgShortPath \tDiameter \tMaxdegree \tAvgDegree \tDegreePower');
for i=1:col
    fprintf(fd,'\tClustering %d',i);
end
fprintf(fd,'\n');
while (nb<=nend)
    logm=zeros(ne-nb+1,col);
for j=1:col
    for i=nb:ne
        logm(i-nb+1,j)=log(a(i,j))-log(a(i-1,j));
    end
end 
    RR=[];
    RR=corrcoef(logm);
    RR=replaceNAN(RR);
    corrmax=max(max(RR));
    corrmin=min(min(RR));
    mas
    distt=[];
    distt=sqrt(2*(1-RR));
    distt=abs(distt);
    RR1=[];
    RR1=prim2(distt);
    shortp=[];
    shortp=floyd(distt);
    asp=[asp,avg_short_path(shortp)];
    diam=[diam,diameter(shortp)];
    clust=[clust,Cluster_Coef(distt,thr)];
    
    maxd=0;
    degr=[];
    [maxd, degr,degr_distr]=degree_distr(RR1);
    %disp(maxd);
    %disp(nb);
    maxdegree=[maxdegree,maxd];
    [coef_a,coef_b]=degredistr2k(degr_distr);
    degree=[degree;degr];
    degree_b=[degree_b,coef_b];
    %dates=[dates,date(ne)];
    nb=nb+step;
    ne=ne+step;
    fprintf(fd,'%d\t%d\t%d\t%d\t%d\t%d\t%d\t',nb,ne,clust(length(clust)),asp(length(asp)),diam(length(diam)),maxd,degr,coef_b);
    for iii=1:col
        clust2=Cluster_Coef2(distt,thr,iii);
        fprintf(fd,'%d\t',clust2);
    end
    fprintf(fd,'\n');    
    if (mod(nb,10)==0)
        fclose(fd);
        fd=fopen('coefs.xls','a');
    end
    %waitbar((nnn-nend+ne)/nnn);//neprawda
    waitbar((nb-nbeg)/nnn);
    %date(ne)
end
close(h);
fclose(fd);
figure;
p=axes;
%plot(dates,clust);
plot(clust);
%set(p,'XTick',[19871020,19871021]);
%set(p,'XTickLabel','21.10.1987 kjh');
title('Clustering');
grid on;
figure;
p=axes;
%plot(dates,asp);
plot(asp);
%set(p,'XTick',19871021);
%set(p,'XTickLabel','21.10.1987');
title('Average short path');
grid on;
figure;
p=axes;
%plot(dates,diam);
plot(diam);
%set(p,'XTick',19871021);
%set(p,'XTickLabel','21.10.1987');
title('Diameter');
grid on;
figure;
p=axes;
%plot(dates,maxdegree);
%set(p,'XTick',19871021);
%set(p,'XTickLabel','21.10.1987');
plot(maxdegree);
title('Max node degree');
grid on;
figure;
p=axes;
%plot(dates,diam);
plot(degree_b);
%set(p,'XTick',19871021);
%set(p,'XTickLabel','21.10.1987');
title('Power of degree distribution');
grid on;

figure;
p=axes;
plot(degree);
title('Average node degree');
grid on;
plot(clust2);
title('Clustering 2');
global coefs;
coefs.clust=clust;
coefs.asp=asp;
coefs.diam=diam;
coefs.diam=diam;
coefs.maxdegree=maxdegree;
coefs.clust2=clust2;