clear all
close all
clc

for i=1:39 % the number of points in the scan
    
    folder=sprintf("C:/ADF_DATA/scan/angle(%d)",i);
    filename1 = sprintf("C:/ADF_DATA/scan/angle(%d)/coord.txt",i);
    fid=fopen(filename1,'r');
    
    j=1;
    l=1;
    while true
        line=fgetl(fid);
        
        if line==-1
            break;
        end
        
        if (j>7 && j<52)  % lines between which the atoms part starts and ends
            str(l)= extractBefore(string(line),"region=PyrDMA");
            l=l+1;
        end
        
        j=j+1;
    end
    
    fclose all;
    
    %%
    
    filename2 = sprintf("SOC.txt");
    fid=fopen(filename2,'r');
    
    j=1;
    l=1;
    while true
        line=fgetl(fid);
        if line==-1
            break;
        end
        
        if (j>8 && j<53) % relacement of atoms
            strn{j}= str(l);
            l=l+1;
        else
            strn{j}=string(line);
        end
        
        j=j+1;
    end
    fclose all;
    
    
    filename3=sprintf("C:/ADF_DATA/scan/angle(%d)/SOC.txt",i);
    fid=fopen(filename3,'w');
    
    for m = 1:numel(strn)
        fprintf(fid,'%s\n', strn{m});
    end
    
    fclose all;
end