function database_write(catena,IDcentralina,DTcatena,X_TuL,Xstar_TuL,Y_TuL,Z_TuL,Zstar_TuL,...
    Xlocal_TuL,Ylocal_TuL,Zlocal_TuL,SegStar,HShift_local_TuL,Area_TuL,...
    Speed_local_TuL,Acceleration_local_TuL,TempDef_TuL,ARRAYdate_TuL,ErrTunnelLink,...
    X_RL,Y_RL,Z_RL,Xlocal_RL,Ylocal_RL,Zlocal_RL,TempDef_RL,ARRAYdate_RL,ErrRadialLink,...
    Y_TLH,Z_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,speed_TLH,...
    speed_local_TLH,acceleration_TLH,acceleration_local_TLH,ARRAYdate_TLH,ErrTiltLinkH,...
    Y_TLHRH,Z_TLHRH,Ylocal_TLHRH,Zlocal_TLHRH,AlfaX_TLHRH,AlfaY_TLHRH,TempDef_TLHRH,speed_TLHRH,...
    speed_local_TLHRH,acceleration_TLHRH,acceleration_local_TLHRH,ARRAYdate_TLHRH,ErrTiltLinkHRH,...
    Y_PCL,Z_PCL,Ylocal_PCL,Zlocal_PCL,AlfaX_PCL,AlfaY_PCL,TempDef_PCL,speed_PCL,...
    speed_local_PCL,acceleration_PCL,acceleration_local_PCL,ARRAYdate_PCL,ErrPreConvLink,...
    Y_PCLHR,Z_PCLHR,Ylocal_PCLHR,Zlocal_PCLHR,AlfaX_PCLHR,AlfaY_PCLHR,TempDef_PCLHR,speed_PCLHR,...
    speed_local_PCLHR,acceleration_PCLHR,acceleration_local_PCLHR,ARRAYdate_PCLHR,ErrPreConvLinkHR,...
    DatiPressure,ARRAYdate_PL,ErrPressureLink,DatiLoad,ARRAYdate_LL,ErrLoadLink,...
    DatiExtensometer,DatiNTCExtensometer,ARRAYdate_EL,Carico_ass,Sforzo,Calcolo_Carico,ErrExtensometerLink,ChEL,EL_NTC,...
    DatiExtensometer3D,DatiNTCExtensometer3D,ARRAYdate_3DEL,Err3DExtensometerLink,Ch3DEL,EL3D_NTC,...
    DatiWireExtensometer,DatiNTCWireExtensometer,ARRAYdate_WEL,ErrWireExtensometerLink,ChWEL,WEL_NTC,...
    DatiMultiBase,ARRAYdate_MPBEL,ErrMultiPointExtensometer,NumBasi,...
    DatiAnalog,~,~,DatiCrack,DatiNTCCrack,ChCrL,CrL_NTC,ARRAYdate_CrL,ErrCrackLink,...
    Dati3DCrack,DatiNTCCrack3D,ARRAYdate_3DCrL,Ch3DCrL,CrL3D_NTC,Err3DCrackLink,...
    Dati2DCrack,DatiNTCCrack2D,ARRAYdate_2DCrL,Ch2DCrL,CrL2D_NTC,Err2DCrackLink,...
    DatiStress,DatiNTCStress,ARRAYdate_SM,ErrStressMeter,ChSM,SM_NTC,...
    conn,FileName)

[scrivoTuL,scrivoRL,scrivoTLH,scrivoTLHRH,scrivoPCL,scrivoPCLHR,scrivoPL,...
    scrivoLL,scrivoEL,scrivo3DEL,scrivoWEL,scrivoMPBEL,~,...
    scrivoCrL,scrivo3DCrL,scrivo2DCrL,scrivoSM] = scrivo(X_TuL,X_RL,...
    Z_TLH,Z_TLHRH,Z_PCL,Z_PCLHR,DatiPressure,DatiLoad,...
    DatiExtensometer,DatiExtensometer3D,DatiWireExtensometer,DatiMultiBase,...
    DatiAnalog,DatiCrack,Dati3DCrack,Dati2DCrack,DatiStress,FileName);

%% Contatori nodi
cTuL = 1; % Tunnel Link
cRL = 1; % Radial Link
cTLH = 1; % Tilt Link H
cTLHRH = 1; % Tilt Link HR H
cPCL = 1; % PreConv Link
cPCLHR = 1; % PreConv Link HR
cPL = 1; % Pressure Link
cLL = 1; % Load Link
cEL = 1; % Extensometer Link
c3DEL = 1; % 3D Extensometer Link
c3DEL_NTC = 1; % NTC 3D Extensometer Link
cWEL = 1; % Wire Extensometer Link
cMPBEL = 1; % Multi Point Rod Extensometer, contatore basi
contMPBEL = 1; % Multi Point Rod Extensometer, contatore singoli nodi
cAL = 1; % Analog Link
cCrL = 1; % Crack Link
c3DCrL = 1; % 3D Crack Link
c2DCrL = 1; % 2D Crack Link
cont3DCrL = 1; % 3D Crack Link, contatore pari a 1/3 del precedente
cSM = 1;

% conto i dati
[~,colTuL] = size(X_TuL); % controllo quanti dati ho
[~,colRL] = size(X_RL); % controllo quanti dati ho
[~,colTLH] = size(Z_TLH); % controllo quanti dati ho
[~,colTLHRH] = size(Z_TLHRH); % controllo quanti dati ho
[~,colPCL] = size(Z_PCL); % controllo quanti dati ho
[~,colPCLHR] = size(Z_PCLHR); % controllo quanti dati ho
[~,colPL] = size(DatiPressure'); % controllo quanti dati ho
[~,colLL] = size(DatiLoad'); % controllo quanti dati ho
[~,colEL] = size(DatiExtensometer'); % controllo quanti dati ho
[~,col3DEL] = size(DatiExtensometer3D'); % controllo quanti dati ho
[~,colWEL] = size(DatiWireExtensometer'); % controllo quanti dati ho
[~,colMPBEL] = size(DatiMultiBase'); % controllo quanti dati ho
[~,colAL] = size(DatiAnalog'); % controllo quanti dati ho
[~,colCrL] = size(DatiCrack'); % controllo quanti dati ho
[~,col3DCrL] = size(Dati3DCrack'); % controllo quanti dati ho
[~,col2DCrL] = size(Dati2DCrack'); % controllo quanti dati ho
[~,colSM] = size(DatiStress'); % controllo quanti dati ho

[rC,~] = size(catena);
idNodo = cell2mat(catena(:,1));

% Apro il file di log
fileID = fopen(FileName,'a');
fmt = '%s \r';

%% Scrittura risultati su DB
% for a = 1:rC % salto l'ancora
for a = 1:rC % salto l'ancora

    NodeNum = cell2mat(catena(a,3));
    
    %% Tunnel Link
    if  idNodo(a,1) == 17
        if scrivoTuL == 1
            ListaDate = colTuL;
            
            DATAinsert = cellMakerTuL(IDcentralina,DTcatena,NodeNum,cTuL,ListaDate,ARRAYdate_TuL,...
                X_TuL,Xstar_TuL,Y_TuL,Z_TuL,Zstar_TuL,Xlocal_TuL,Ylocal_TuL,Zlocal_TuL,...
                SegStar,HShift_local_TuL,TempDef_TuL,Area_TuL,Speed_local_TuL,Acceleration_local_TuL,ErrTunnelLink);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_TuL,conn);
            
            DBwriteTuL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cTuL = cTuL+1;
            clear DATAinsert
            % Logfile
            text = ['Tunnel Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Radial Link
    elseif  idNodo(a,1) == 18
        if scrivoRL == 1
            ListaDate = colRL;
            
            DATAinsert = cellMakerRL(IDcentralina,DTcatena,NodeNum,cRL,ListaDate,ARRAYdate_RL,...
                X_RL,Y_RL,Z_RL,Xlocal_RL,Ylocal_RL,Zlocal_RL,TempDef_RL,ErrRadialLink);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_RL,conn);
            
            DBwriteRL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cRL = cRL+1;
            clear DATAinsert
            % Logfile
            text = ['Radial Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Tilt Link H
    elseif  idNodo(a,1) == 11
        if scrivoTLH == 1
            ListaDate = colTLH;
            
            DATAinsert = cellMakerTLH(IDcentralina,DTcatena,NodeNum,cTLH,ListaDate,...
                Y_TLH,Z_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,speed_TLH,...
                speed_local_TLH,acceleration_TLH,acceleration_local_TLH,ErrTiltLinkH,ARRAYdate_TLH);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_TLH,conn);
            
            DBwriteTLH(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cTLH = cTLH+1;
            clear DATAinsert
            % Logfile
            text = ['Tilt Link H node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Tilt Link HR H
    elseif  idNodo(a,1) == 12
        if scrivoTLHRH == 1
            ListaDate = colTLHRH;
            
            DATAinsert = cellMakerTLHRH(IDcentralina,DTcatena,NodeNum,cTLHRH,ListaDate,...
                Y_TLHRH,Z_TLHRH,Ylocal_TLHRH,Zlocal_TLHRH,AlfaX_TLHRH,AlfaY_TLHRH,TempDef_TLHRH,...
                speed_TLHRH,speed_local_TLHRH,acceleration_TLHRH,acceleration_local_TLHRH,...
                ErrTiltLinkHRH,ARRAYdate_TLHRH);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_TLHRH,conn);
            
            DBwriteTLHRH(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cTLHRH = cTLHRH+1;
            clear DATAinsert
            % Logfile
            text = ['Tilt Link HR H node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% PreConv Link
    elseif  idNodo(a,1) == 23
        if scrivoPCL == 1
            ListaDate = colPCL;
            
            DATAinsert = cellMakerPCL(IDcentralina,DTcatena,NodeNum,cPCL,ListaDate,...
                Y_PCL,Z_PCL,Ylocal_PCL,Zlocal_PCL,AlfaX_PCL,AlfaY_PCL,TempDef_PCL,speed_PCL,...
                speed_local_PCL,acceleration_PCL,acceleration_local_PCL,ErrPreConvLink,ARRAYdate_PCL);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_PCL,conn);
            
            DBwritePCL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cPCL = cPCL+1;
            clear DATAinsert
            % Logfile
            text = ['PreConv Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ' of unit ' IDcentralina ' and chain ' ...
                DTcatena ' written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% PreConv Link HR
    elseif  idNodo(a,1) == 24
        if scrivoPCLHR == 1
            ListaDate = colPCLHR;
            
            DATAinsert = cellMakerPCLHR(IDcentralina,DTcatena,NodeNum,cPCLHR,ListaDate,...
                Y_PCLHR,Z_PCLHR,Ylocal_PCLHR,Zlocal_PCLHR,AlfaX_PCLHR,AlfaY_PCLHR,TempDef_PCLHR,...
                speed_PCLHR,speed_local_PCLHR,acceleration_PCLHR,acceleration_local_PCLHR,...
                ErrPreConvLinkHR,ARRAYdate_PCLHR);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_PCLHR,conn);
            
            DBwritePCLHR(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cPCLHR = cPCLHR+1;
            clear DATAinsert
            % Logfile
            text = ['PreConv Link HR node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ' of unit ' IDcentralina ' and chain ' ...
                DTcatena ' written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Pressure Link
    elseif  idNodo(a,1) == 21
        if scrivoPL == 1
            ListaDate = colPL;
            
            DATAinsert = cellMakerPL(IDcentralina,DTcatena,NodeNum,cPL,ListaDate,...
                ARRAYdate_PL,DatiPressure,ErrPressureLink);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_PL,conn);
            
            DBwritePL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cPL = cPL+1;
            clear DATAinsert
            % Logfile
            text = ['Pressure Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ' of unit ' IDcentralina ' and chain ' ...
                DTcatena ' written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Load Link
    elseif  idNodo(a,1) == 15
        if scrivoLL == 1
            ListaDate = colLL;
            
            DATAinsert = cellMakerLL(IDcentralina,DTcatena,NodeNum,cLL,ListaDate,...
                ARRAYdate_LL,DatiLoad,ErrLoadLink);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_LL,conn);
            
            DBwriteLL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,conn);
            
            cLL = cLL+1;
            clear DATAinsert
            % Logfile
            text = ['Load Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ' of unit ' IDcentralina ' and chain ' ...
                DTcatena ' written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Extensometer Link
    elseif  idNodo(a,1) == 16
        if scrivoEL == 1
            ListaDate = colEL;
            
            DATAinsert = cellMakerEL(IDcentralina,DTcatena,NodeNum,cEL,ListaDate,...
                ARRAYdate_EL,DatiExtensometer,DatiNTCExtensometer,Carico_ass,Sforzo,...
                Calcolo_Carico,ErrExtensometerLink,ChEL,EL_NTC);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,...
                ListaDate,ARRAYdate_EL,conn);
            
            DBwriteEL(DATAinsert,idElabData,ListaDate,NodeNum,Calcolo_Carico,...
                IDcentralina,DTcatena,cEL,ChEL,EL_NTC,conn);
            
            cEL = cEL+1;
            clear DATAinsert
            % Logfile
            text = ['Extensometer Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% 3D Extensometer Link
    elseif  idNodo(a,1) == 19
        if scrivo3DEL == 1
            ListaDate = col3DEL;
            
            DATAinsert = cellMaker3DEL(IDcentralina,DTcatena,NodeNum,c3DEL,c3DEL_NTC,ListaDate,ARRAYdate_3DEL,...
                DatiExtensometer3D,DatiNTCExtensometer3D,Err3DExtensometerLink,Ch3DEL,EL3D_NTC);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_3DEL,conn);
            
            DBwrite3DEL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,...
                DTcatena,Ch3DEL,c3DEL_NTC,EL3D_NTC,conn);
            
            c3DEL = c3DEL+3;
            c3DEL_NTC = c3DEL_NTC+1;
            clear DATAinsert
            % Logfile
            text = ['3D Extensometer Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Wire Extensometer Link
    elseif  idNodo(a,1) == 22
        if scrivoWEL == 1
            ListaDate = colWEL;
            
            DATAinsert = cellMakerWEL(IDcentralina,DTcatena,NodeNum,cWEL,ListaDate,...
                ARRAYdate_WEL,DatiWireExtensometer,DatiNTCWireExtensometer,ChWEL,WEL_NTC,...
                ErrWireExtensometerLink);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_WEL,conn);
            
            DBwriteWEL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,DTcatena,...
                cWEL,ChWEL,WEL_NTC,conn);
            
            cWEL = cWEL+1;
            clear DATAinsert
            % Logfile
            text = ['Wire Extensometer Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Crack Link
    elseif  idNodo(a,1) == 36
        if scrivoCrL == 1
            ListaDate = colCrL;
            
            DATAinsert = cellMakerCrL(IDcentralina,DTcatena,NodeNum,cCrL,ListaDate,...
                ARRAYdate_CrL,DatiCrack,DatiNTCCrack,ChCrL,CrL_NTC,ErrCrackLink);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_CrL,conn);
            
            DBwriteCrL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,...
                DTcatena,cCrL,ChCrL,CrL_NTC,conn);
            
            cCrL = cCrL+1;
            clear DATAinsert
            % Logfile
            text = ['Crack Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% 3D Crack Link
    elseif  idNodo(a,1) == 37
        if scrivo3DCrL == 1
            ListaDate = col3DCrL;
            
            DATAinsert = cellMaker3DCrL(IDcentralina,DTcatena,NodeNum,c3DCrL,ListaDate,...
                ARRAYdate_3DCrL,Dati3DCrack,DatiNTCCrack3D,Err3DCrackLink,...
                Ch3DCrL,CrL3D_NTC,cont3DCrL);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_3DCrL,conn);
            
            DBwrite3DCrL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,...
                DTcatena,Ch3DCrL,CrL3D_NTC,cont3DCrL,conn);
            
            c3DCrL = c3DCrL+3;
            cont3DCrL = cont3DCrL+1;
            clear DATAinsert
            % Logfile
            text = ['3D Crack Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% 2D Crack Link
    elseif  idNodo(a,1) == 51
        if scrivo2DCrL == 1
            ListaDate = col2DCrL;
            
            DATAinsert = cellMaker2DCrL(IDcentralina,DTcatena,NodeNum,c2DCrL,ListaDate,...
                ARRAYdate_2DCrL,Dati2DCrack,DatiNTCCrack2D,Err2DCrackLink,...
                Ch2DCrL,CrL2D_NTC,cont2DCrL);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_2DCrL,conn);
            
            DBwrite3DCrL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,...
                DTcatena,Ch3DCrL,CrL3D_NTC,cont3DCrL,conn);
            
            c2DCrL = c2DCrL+2;
            cont3DCrL = cont3DCrL+1;
            clear DATAinsert
            % Logfile
            text = ['2D Crack Link node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% MultiPoint BoreHole Extensometer
    elseif  idNodo(a,1) == 25
        if scrivoMPBEL == 1
            ListaDate = colMPBEL;
            
            DATAinsert = cellMakerMPBEL(IDcentralina,DTcatena,NodeNum,cMPBEL,contMPBEL,...
                ListaDate,ARRAYdate_MPBEL,DatiMultiBase,NumBasi,ErrMultiPointExtensometer);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_MPBEL,conn);
            
            DBwriteMPBEL(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,...
                DTcatena,NumBasi,conn);
            
            cMPBEL = cMPBEL+NumBasi;
            contMPBEL = contMPBEL+1;
            clear DATAinsert
            % Logfile
            text = ['Multi Point Borehole Extensometer node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    %% Stress Meter
    elseif  idNodo(a,1) == 47
        if scrivoSM == 1
            ListaDate = colSM;
            
            DATAinsert = cellMakerSM(IDcentralina,DTcatena,NodeNum,cSM,ListaDate,...
                ARRAYdate_SM,DatiStress,DatiNTCStress,ChSM,SM_NTC,ErrStressMeter);
            
            idElabData = DB_date_time(IDcentralina,DTcatena,NodeNum,ListaDate,ARRAYdate_SM,conn);
            
            DBwriteSM(DATAinsert,idElabData,ListaDate,NodeNum,IDcentralina,...
                DTcatena,ChSM,SM_NTC,cSM,conn);
            
            cSM = cSM+1;
            clear DATAinsert
            % Logfile
            text = ['Stress Meter node no. ' num2str(NodeNum) ' of ' ...
                num2str(rC) ', unit ' IDcentralina ' and chain ' DTcatena ...
                ', has been written in the DB!'];
            fprintf(fileID,fmt,text);
        end
        
    end
end

text = 'All the data have been written in the DB correctly';
fprintf(fileID,fmt,text);
fclose(fileID);

end
