% Questa funzione legge i dati di ogni tipologia di nodo (se presente)
% DatiTunnelLink contiene tutti i dati per i Tilt Link
% DatiRadialLink contiene tutti i dati per i Radial Link
% etc.

function [Batteria,DatiTunnelLink,ErrTunnelLink,datainiTuL,...
    DatiRadialLink,ErrRadialLink,datainiRL,DatiTiltLinkH,ErrTiltLinkH,datainiTLH,...
    DatiTiltLinkHRH,ErrTiltLinkHRH,datainiTLHRH,DatiPreConvLink,ErrPreConvLink,datainiPCL,...
    DatiPreConvLinkHR,ErrPreConvLinkHR,datainiPCLHR,DatiDistoMTLink,ErrDistoMTLink,datainiDM,...
    DatiPressureLink,ErrPressureLink,...
    DatiLoadLink,ErrLoadLink,DatiExtensometerLink,DatiExtensometerLink_Rif,...
    DatiNTCExtensometerLink,ErrExtensometerLink,Dati3DExtensometerLink,ChEL,EL_NTC,...
    Dati3DExtensometerLink_Rif,DatiNTC3DExtensometerLink,Err3DExtensometerLink,ErrNTC3DExtensometerLink,Ch3DEL,EL3D_NTC,...
    DatiWireExtensometerLink,DatiNTCWireExtensometerLink,ErrWireExtensometerLink,ChWEL,WEL_NTC,...
    DatiMultiPointExtensometer,DatiNTCMultiPointExtensometer,ErrMultiPointExtensometer,ErrNTCMultiPointExtensometer,ChMPBEL,MPBEL_NTC,...
    DatiAnalogLink,ErrAnalogLink,datainiAL,ChAL,AL_NTC,...
    DatiCrackLink,DatiNTCCrackLink,ErrCrackLink,ChCrL,CrL_NTC,...
    Dati3DCrackLink,DatiNTC3DCrackLink,Err3DCrackLink,ErrNTC3DCrackLink,Ch3DCrL,CrL3D_NTC,...
    Dati2DCrackLink,DatiNTC2DCrackLink,Err2DCrackLink,ErrNTC2DCrackLink,Ch2DCrL,CrL2D_NTC,...
    DatiStressMeter,DatiStressMeter_Rif,DatiNTCStressMeter,DatiNTCStressMeter_Rif,ErrStressMeter,ChSM,SM_NTC,...
    yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,...
    yesWEL,yesMPBEL,yesAL,yesCrL,yes3DCrL,yes2DCrL,yesSM,yesDM] = ...
    lettura(IDcentralina,DTcatena,datainiTuL,tempoiniTuL,NodoTunnelLink,NuovoZeroTuL,date_AC,time_AC,Nodo_AC,...
    datainiRL,tempoiniRL,NodoRadialLink,NuovoZeroRL,datainiTLH,tempoiniTLH,NodoTiltLinkH,NuovoZeroTLH,...
    datainiTLHRH,tempoiniTLHRH,NodoTiltLinkHRH,NuovoZeroTLHRH,datainiPCL,tempoiniPCL,NodoPreConvLink,NuovoZeroPCL,...
    datainiPCLHR,tempoiniPCLHR,NodoPreConvLinkHR,NuovoZeroPCLHR,datainiDM,tempoiniDM,NodoDistoMTLink,NuovoZeroDM,...
    datainiPL,tempoiniPL,NodoPressureLink,NuovoZeroPrL,datainiLL,tempoiniLL,NodoLoadLink,NuovoZeroLL,...
    datainiEL,tempoiniEL,Date_Rif_EL,NodoExtensometerLink,NuovoZeroEL,...
    dataini3DEL,tempoini3DEL,Date_Rif_3DEL,Nodo3DExtensometerLink,NuovoZero3DEL,...
    datainiWEL,tempoiniWEL,NodoWireExtensometerLink,NuovoZeroWEL,...
    datainiMPBEL,tempoiniMPBEL,NodoMultiPointRodExtensometer,NuovoZeroMPBEL,...
    datainiAL,tempoiniAL,NodoAnalogLink,NumBasi,NuovoZeroAL,...
    datainiCrL,tempoiniCrL,NodoCrackLink,NuovoZeroCrL,dataini3DCrL,tempoini3DCrL,Nodo3DCrackLink,NuovoZero3DCrL,...
    dataini2DCrL,tempoini2DCrL,Nodo2DCrackLink,NuovoZero2DCrL,...
    datainiSM,tempoiniSM,Date_Rif_SM,NodoStressMeter,NuovoZeroSM,yesTuL,yesRL,yesTLH,...
    yesTLHRH,yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesAL,yesCrL,...
    yes3DCrL,yes2DCrL,yesSM,yesDM,rTuL,rRL,rTLH,rTLHRH,rPCL,rPCLHR,rPrL,...
    rLL,rEL,r3DEL,rWEL,rMPBEL,rAL,rCrL,r3DCrL,r2DCrL,rSM,rDM,conn,catena,date,time,FileName)

% Log file
fileID = fopen(FileName,'a');
fmt = '%s \r';
text = 'lettura function started';
fprintf(fileID,fmt,text);

%% Batteria
% cerco la batteria nel primo nodo
j = 1;
z = 0;
[rC,~] = size(catena);
while z == 0
    if cell2mat(catena(j,3)) == 1
        z = 1;
    else
        j = j+1; % nodo numero 1
    end
end
condition = 0;
while condition == 0
    if j <= rC
        i = cell2mat(catena(j,1));
        if i == 17
            dataini = datainiTuL;
            tempoini = tempoiniTuL;
            break
        elseif i == 18
            dataini = datainiRL;
            tempoini = tempoiniRL;
            break
        elseif i == 11
            dataini = datainiTLH;
            tempoini = tempoiniTLH;
            break
        elseif i == 12
            dataini = datainiTLHRH;
            tempoini = tempoiniTLHRH;
            break
        elseif i == 23
            dataini = datainiPCL;
            tempoini = tempoiniPCL;
            break
        elseif i == 24
            dataini = datainiPCLHR;
            tempoini = tempoiniPCLHR;
            break
        elseif i == 8
            dataini = datainiAL;
            tempoini = tempoiniAL;
            break
        elseif i == 15
            dataini = datainiLL;
            tempoini = tempoiniLL;
            break
        elseif i == 16
            dataini = datainiEL;
            tempoini = tempoiniEL;
            break
        elseif i == 19
            dataini = dataini3DEL;
            tempoini = tempoini3DEL;
            break
        elseif i == 21
            dataini = datainiPL;
            tempoini = tempoiniPL;
            break
        elseif i == 22
            dataini = datainiWEL;
            tempoini = tempoiniWEL;
            break
        elseif i == 25
            dataini = datainiMPBEL;
            tempoini = tempoiniMPBEL;
            break
        elseif i == 36
            dataini = datainiCrL;
            tempoini = tempoiniCrL;
            break
        elseif i == 37
            dataini = dataini3DCrL;
            tempoini = tempoini3DCrL;
            break
        elseif i == 51
            dataini = dataini2DCrL;
            tempoini = tempoini2DCrL;
            break
        elseif i == 47
            dataini = datainiSM;
            tempoini = tempoiniSM;
            break
        elseif i == 56
            dataini = datainiDM;
            tempoini = tempoiniDM;
            break
        end
        j = j+1;
    else
        break
    end
end

NodeNum = '1';
comando = ['select Date, Time, BatLevel from RawDataView where Date = ''' ...
        dataini ''' and Time >= ''' tempoini ''' and  ToolNameID = ''' DTcatena...
        ''' and UnitName = ''' IDcentralina ''' and NodeNum = ''' NodeNum ''' '];
curs = exec(conn,comando);
curs = fetch(curs);
BatTempo = curs.Data;
comando = ['select Date, Time, BatLevel from RawDataView where Date > ''' ...
        dataini ''' and  ToolNameID = ''' DTcatena...
        ''' and UnitName = ''' IDcentralina ''' and NodeNum = ''' NodeNum ''' '];
curs = exec(conn,comando);
curs = fetch(curs);
BatData = curs.Data;
if strcmp(char(BatData(1)),'No Data')
   Batteria = BatTempo;
elseif strcmp(char(BatTempo(1)),'No Data')
        Batteria = BatData;
else
    Batteria = [BatTempo; BatData];
end

Data(1,1) = cellstr(date);
Data(1,2) = cellstr(time);

%%  Tunnel Link
if  yesTuL == 0 % Non ci sono Tunnel Link
    DatiTunnelLink = [];
    ErrTunnelLink = [];
    wardat = 'lettura function: there are not Tunnel Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-TuL-RawData.csv'];
    NAN = 0;
    date_AC = [date_AC ' ' time_AC];
    while NAN == 0
        if datenum(datainiTuL) == datenum(Data(1,1))
            tempoiniTuL = char(Data(1,2));
        end
        NodeType = 'Tunnel Link';
        NodeNumTuL = num2str(cell2mat(NodoTunnelLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiTuL ''' and Time >= ''' tempoiniTuL ''' and  ToolNameID = ''' DTcatena...
            ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumTuL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniTuL = curs.Data;
        Ntempo = DATiniTuL;
        [r,c] = size(DATiniTuL);
        saltoTuL = 0;
        if  r==1 && c==1
            textdatTuL = 'Warning: error reading Tunnel Link (control unit data)!';
            fprintf(fileID,fmt,textdatTuL);
            saltoTuL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiTuL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumTuL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2TuL = curs.Data;
        Ndate = DAT2TuL;
        if saltoTuL == 1
            DATiniTuL = DAT2TuL;
        end
        Check = size(DAT2TuL);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoTuL == 0
                DATiniTuL = [DATiniTuL; DAT2TuL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniTuL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniTuL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniTuL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniTuL = T;
        else
            DATiniTuL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Tunnel Link
        if saltoTuL == 0
            if Check(1,2) > 1 % ho superato il primo giorno
                DatiTunnelLink = cell(rd+rt,4*rTuL);
            else
                DatiTunnelLink = cell(rt,4*rTuL);
            end
        else
            DatiTunnelLink = cell(rd,4*rTuL);
        end
        NTunnelLink = cell2mat(NodoTunnelLink(:,2));
        col = 4;
        ii = 1;
        w = 0;
        if now < datenum(date_AC) % NON si elabora Arco Rovescio
            Arco = 0;
        else
            Arco = 1;
        end
        while ii <= rTuL
            if ii >= Nodo_AC % Primo nodo arco rovescio
                if Arco == 1
                    Continua = 1;
                else
                    Continua = 0;
                end
            else
                Continua = 1;
            end
            if Continua == 1
                % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
                % ma limitandomi a quelli registrati nel corso di quella singola giornata;
                nN = NTunnelLink(ii,1);
                nNodo = num2str(nN);
                comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date = ''' ...
                    datainiTuL ''' and Time >= ''' tempoiniTuL ''' and UnitName = ''' IDcentralina...
                    ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTuL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date > ''' ...
                    datainiTuL ''' and UnitName = ''' IDcentralina ...
                    ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTuLd = curs.Data;
            end
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            ini_col = col-3;
            if saltoTuL == 0
                if Continua == 1
                    DatiTunnelLink(1:rt,ini_col:col) = DATnodoTuL(:,:);
                else
                    DatiTunnelLink(1:rt,ini_col:col) = cellstr('0');
                end
            else
                if Continua == 1
                    DatiTunnelLink(1:rd,ini_col:col) = DATnodoTuLd(:,:);
                else
                    DatiTunnelLink(1:rd,ini_col:col) = cellstr('0');
                end
            end
            
            [rn,~] = size(DATnodoTuLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoTuLd(i,:) = DATnodoTuLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Tunnel Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoTuL == 0
                    if Continua == 1
                        DatiTunnelLink(rt+1:rt+rd,ini_col:col) = DATnodoTuLd(:,:);
                    else
                        DatiTunnelLink(rt+1:rt+rd,ini_col:col) = cellstr('0');
                    end
                end
            else
                DatiTunnelLink = DatiTunnelLink(1:end,:);
            end
            okdatTuL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rTuL) ' Tunnel Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatTuL);
            ii = ii+1;
            col = col+4;
        end
        if w == 0
            wardat = 'There are not warning for Tunnel Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiTunnelLink);
        ErrTunnelLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroTuL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiTuL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiTunnelLink(1,jj)));
            idxND = find(strcmp('No Data',DatiTunnelLink(1,jj)));
            idxT = find(strcmp('---',DatiTunnelLink(1,jj)));
            idxT2 = find(strcmp('-',DatiTunnelLink(1,jj)));
            idxE2 = find(strcmp('Err2',DatiTunnelLink(1,jj)));
            idxE255 = find(strcmp('Err255',DatiTunnelLink(1,jj)));
            idxD = find(strcmp('DMUXe',DatiTunnelLink(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiTunnelLink(1,jj)));
            idxN = find(strcmp('null',DatiTunnelLink(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of Tunnel Link nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroTuL == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiTuL)+1 < now
                        datainiTuL = datestr(datenum(datainiTuL)+1,'yyyy-mm-dd');
                        break
                    else
                        yesTuL = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiTuL = datestr(datenum(datainiTuL)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiTunnelLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrTunnelLink(1,jj) = 1;
                            wardat = 'Data of Tunnel Link nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiTuL = datestr(datenum(datainiTuL)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c % nodi
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiTunnelLink(:,jj)));
                idxND = find(strcmp('No Data',DatiTunnelLink(:,jj)));
                idxT = find(strcmp('---',DatiTunnelLink(:,jj)));
                idxT2 = find(strcmp('-',DatiTunnelLink(:,jj)));
                idxE2 = find(strcmp('Err2',DatiTunnelLink(:,jj)));
                idxE255 = find(strcmp('Err255',DatiTunnelLink(:,jj)));
                idxD = find(strcmp('DMUXe',DatiTunnelLink(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiTunnelLink(:,jj)));
                idxN = find(strcmp('null',DatiTunnelLink(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiTuL = datestr(datenum(datainiTuL)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiTunnelLink(indice,jj) = DatiTunnelLink(indice-1,jj);
                    ErrTunnelLink(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiTunnelLink(nnn,jj)));
                    if check == 1
                        DatiTunnelLink(nnn,jj) = DatiTunnelLink(nnn-1,jj);
                        ErrTunnelLink(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
            corrdat = ['' num2str(Ncorr) ' corrections of Tunnel Link data done!'];
            fprintf(fileID,fmt,corrdat);
            % Concateno informazioni generali e/o letture dai nodi.
            DatiTunnelLink = [DATiniTuL str2double(DatiTunnelLink)];
            % Elimino eventuali date doppie
            a = 2;
            while a <= r
                if DatiTunnelLink(a,1) == DatiTunnelLink(a-1,1)
                    DatiTunnelLink(a,:) = [];
                    r = r-1;
                else
                    a = a+1;
                end
            end
        end
    end
    if NuovoZeroTuL == 1
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiTunnelLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiTunnelLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiTunnelLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiTunnelLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiTunnelLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiTunnelLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end  

%%  Radial Link
if  yesRL == 0 % Non ci sono Radial Link
    DatiRadialLink = [];
    ErrRadialLink = [];
    wardat = 'lettura function: there are not Radial Link';
    fprintf(fileID,fmt,wardat);
else
    NodeType = 'Radial Link';
    RawDataFile = ['' IDcentralina '-' DTcatena '-RL-RawData.csv'];
    NAN = 0;
    while NAN == 0
        if datenum(datainiRL) == datenum(Data(1,1))
            tempoiniRL = char(Data(1,2));
        end
        NodeNumRL = num2str(cell2mat(NodoRadialLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiRL ''' and Time >= ''' tempoiniRL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumRL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniRL = curs.Data;
        Ntempo = DATiniRL;
        [r,c] = size(DATiniRL);
        saltoRL = 0;
        if  r==1 && c==1
            textdatRL = 'Warning: error reading Radial Link (control unit data)!';
            fprintf(fileID,fmt,textdatRL);
            saltoRL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiRL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumRL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2RL = curs.Data;
        Ndate = DAT2RL;
        if saltoRL == 1
            DATiniRL = DAT2RL;
        end
        Check = size(DAT2RL);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoRL == 0
                DATiniRL = [DATiniRL; DAT2RL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniRL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniRL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniRL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniRL = T;
        else
            DATiniRL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        if saltoRL == 0
            % creo la matrice per i dati Radial Link
            if Check(1,2) > 1
                DatiRadialLink = cell(rd+rt,4*rRL);
            else
                DatiRadialLink = cell(rt,4*rRL);
            end
        else
            DatiRadialLink = cell(rd,4*rRL);
        end
        NRadialLink = cell2mat(NodoRadialLink(:,2));
        col = 4;
        ii = 1;
        w = 0;
        while ii <= rRL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NRadialLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date = ''' ...
                datainiRL ''' and Time >= ''' tempoiniRL ''' and UnitName = ''' IDcentralina ...
                ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRL = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date > ''' ...
                datainiRL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRLd = curs.Data;
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            ini_col = col-3;
            if saltoRL == 0
                DatiRadialLink(1:rt,ini_col:col) = DATnodoRL(:,:);
            else
                DatiRadialLink(1:rd,ini_col:col) = DATnodoRL(:,:);
            end
            
            [rn,~] = size(DATnodoRLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoRLd(i,:) = DATnodoRLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Radial Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoRL == 0
                    DatiRadialLink(rt+1:rt+rd,ini_col:col) = DATnodoRLd(:,:);
                end
            else
                DatiRadialLink = DatiRadialLink(1:end,:);
            end
            okdatRL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rRL) ' Radial Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatRL);
            ii = ii+1;
            col = col+4;
        end
        if w == 0
            wardat = 'There are not warning for Radial Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiRadialLink);
        ErrRadialLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroRL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiRL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiRadialLink(1,jj)));
            idxND = find(strcmp('No Data',DatiRadialLink(1,jj)));
            idxT = find(strcmp('---',DatiRadialLink(1,jj)));
            idxT2 = find(strcmp('-',DatiRadialLink(1,jj)));
            idxE2 = find(strcmp('Err2',DatiRadialLink(1,jj)));
            idxE255 = find(strcmp('Err255',DatiRadialLink(1,jj)));
            idxD = find(strcmp('DMUXe',DatiRadialLink(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiRadialLink(1,jj)));
            idxN = find(strcmp('null',DatiRadialLink(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of Radial Link nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroRL == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiRL)+1 < now
                        datainiRL = datestr(datenum(datainiRL)+1,'yyyy-mm-dd');
                        break
                    else
                        yesRL = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiRL = datestr(datenum(datainiRL)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiRadialLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrRadialLink(1,jj) = 1;
                            wardat = 'Data of Radial Link nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiRL = datestr(datenum(datainiRL)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiRadialLink(:,jj)));
                idxND = find(strcmp('No Data',DatiRadialLink(:,jj)));
                idxT = find(strcmp('---',DatiRadialLink(:,jj)));
                idxT2 = find(strcmp('-',DatiRadialLink(:,jj)));
                idxE2 = find(strcmp('Err2',DatiRadialLink(:,jj)));
                idxE255 = find(strcmp('Err255',DatiRadialLink(:,jj)));
                idxD = find(strcmp('DMUXe',DatiRadialLink(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiRadialLink(:,jj)));
                idxN = find(strcmp('null',DatiRadialLink(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiRL = datestr(datenum(datainiRL)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiRadialLink(indice,jj) = DatiRadialLink(indice-1,jj);
                    ErrRadialLink(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiRadialLink(nnn,jj)));
                    if check == 1
                        DatiRadialLink(nnn,jj) = DatiRadialLink(nnn-1,jj);
                        ErrRadialLink(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
            corrdat = ['' num2str(Ncorr) ' corrections of Radial Link data done!'];
            fprintf(fileID,fmt,corrdat);
            % Concateno informazioni generali e/o letture dai nodi.
            DatiRadialLink = [DATiniRL str2double(DatiRadialLink)];
            % Elimino eventuali date doppie
            a = 2;
            while a <= r
                if DatiRadialLink(a,1) == DatiRadialLink(a-1,1)
                    DatiRadialLink(a,:) = [];
                    r = r-1;
                else
                    a = a+1;
                end
            end
        end
    end
    if NuovoZeroRL == 1
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiRadialLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiRadialLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiRadialLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiRadialLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiRadialLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiRadialLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end  

%% Tilt Link H
if  yesTLH == 0 % Non ci sono Tilt Link H
    DatiTiltLinkH = [];
    ErrTiltLinkH = [];
    wardat = 'lettura function: there are not Tilt Link H';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-TLH-RawData.csv'];
    NAN = 0;
    while NAN == 0
        if datenum(datainiTLH) == datenum(Data(1,1))
            tempoiniTLH = char(Data(1,2));
        end
        NodeType = 'Tilt Link H';
        NodeNumTLH = num2str(cell2mat(NodoTiltLinkH(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiTLH ''' and Time >= ''' tempoiniTLH ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ...
            ''' and NodeNum = ''' NodeNumTLH ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniTLH = curs.Data;
        Ntempo = DATiniTLH;
        [r,c] = size(DATiniTLH);
        saltoTLH = 0;
        if  r==1 && c==1
            textdatTLH = 'Warning: error reading Tilt Link H (control unit data)!';
            fprintf(fileID,fmt,textdatTLH);
            saltoTLH = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiTLH ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumTLH ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2TLH = curs.Data;
        Ndate = DAT2TLH;
        if saltoTLH == 1
            DATiniTLH = DAT2TLH;
        end
        Check = size(DAT2TLH);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoTLH == 0
                DATiniTLH = [DATiniTLH; DAT2TLH];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniTLH);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniTLH(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniTLH(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniTLH = T;
        else
            DATiniTLH = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Tunnel Link
        if saltoTLH == 0
            if Check(1,2) > 1 % ho superato il primo giorno
                DatiTiltLinkH = cell(rd+rt,4*rTLH);
            else
                DatiTiltLinkH = cell(rt,4*rTLH);
            end
        else
            DatiTiltLinkH = cell(rd,4*rTLH);
        end
        NTiltLinkH = cell2mat(NodoTiltLinkH(:,2));
        col = 4;
        ii = 1;
        w = 0;
        while ii <= rTLH
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NTiltLinkH(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date = ''' ...
                datainiTLH ''' and Time >= ''' tempoiniTLH ''' and UnitName = ''' IDcentralina ''' and  ToolNameID = '''...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoTLH = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date > ''' ...
                datainiTLH ''' and ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ...
                ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoTLHd = curs.Data;
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            ini_col = col-3;
            if saltoTLH == 0
                DatiTiltLinkH(1:rt,ini_col:col) = DATnodoTLH(:,:);
            else
                DatiTiltLinkH(1:rd,ini_col:col) = DATnodoTLHd(:,:);
            end
            
            [rn,~] = size(DATnodoTLHd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoTLHd(i,:) = DATnodoTLHd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Tilt Link H ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoTLH == 0
                    DatiTiltLinkH(rt+1:rt+rd,ini_col:col) = DATnodoTLHd(:,:);
                end
            else
                DatiTiltLinkH = DatiTiltLinkH(1:end,:);
            end
            okdatTLH = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rTLH) ' Tilt Link H nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatTLH);
            ii = ii+1;
            col = col+4;
        end
        if w == 0
            wardat = 'There are not warning for Tilt Link H!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiTiltLinkH);
        ErrTiltLinkH = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroTLH == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiTLH));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiTiltLinkH(1,jj)));
            idxND = find(strcmp('No Data',DatiTiltLinkH(1,jj)));
            idxT = find(strcmp('---',DatiTiltLinkH(1,jj)));
            idxT2 = find(strcmp('-',DatiTiltLinkH(1,jj)));
            idxE2 = find(strcmp('Err2',DatiTiltLinkH(1,jj)));
            idxE255 = find(strcmp('Err255',DatiTiltLinkH(1,jj)));
            idxD = find(strcmp('DMUXe',DatiTiltLinkH(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiTiltLinkH(1,jj)));
            idxN = find(strcmp('null',DatiTiltLinkH(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of Tilt Link H nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroTLH == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiTLH)+1 < now
                        datainiTLH = datestr(datenum(datainiTLH)+1,'yyyy-mm-dd');
                        break
                    else
                        yesTLH = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiTLH = datestr(datenum(datainiTLH)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiTiltLinkH(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrTiltLinkH(1,jj) = 1;
                            wardat = 'Data of Tilt Link H nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiTLH = datestr(datenum(datainiTLH)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
            if ESCI == 0
                % Cerco i multipli di 4 per identificare le temperature
                Int = jj/4;
                if floor(Int) == Int
                    % Controllo ampolle
                    if str2double(DatiTiltLinkH(1,jj)) > 1000
                        if rDR==1 && cDR==1 || NuovoZeroTLH == 0
                            NAN = 0;
                            ESCI = 1;
                            if datenum(datainiTLH)+1 < now
                                datainiTLH = datestr(datenum(datainiTLH)+1,'yyyy-mm-dd');
                                break
                            else
                                yesTLH = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiTLH = datestr(datenum(datainiTLH)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiTiltLinkH(1,jj-3) = cellstr(num2str(DatiRaw(RawDate(end),jj-2)));
                                DatiTiltLinkH(1,jj-2) = cellstr(num2str(DatiRaw(RawDate(end),jj-1)));
                                DatiTiltLinkH(1,jj-1) = cellstr(num2str(DatiRaw(RawDate(end),jj)));
                                DatiTiltLinkH(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrTiltLinkH(1,jj) = 1;
                                wardat = ['Data of Tilt Link H nodes corrected using Raw Data of reference Csv file. '...
                                    'MEMS was read as an electrolytic cell'];
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiTiltLinkH(:,jj)));
                idxND = find(strcmp('No Data',DatiTiltLinkH(:,jj)));
                idxT = find(strcmp('---',DatiTiltLinkH(:,jj)));
                idxT2 = find(strcmp('-',DatiTiltLinkH(:,jj)));
                idxE2 = find(strcmp('Err2',DatiTiltLinkH(:,jj)));
                idxE255 = find(strcmp('Err255',DatiTiltLinkH(:,jj)));
                idxD = find(strcmp('DMUXe',DatiTiltLinkH(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiTiltLinkH(:,jj)));
                idxN = find(strcmp('null',DatiTiltLinkH(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiTLH = datestr(datenum(datainiTLH)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiTiltLinkH(indice,jj) = DatiTiltLinkH(indice-1,jj);
                    ErrTiltLinkH(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                for ii = 2:r
                    % Cerco i multipli di 7 per identificare le temperature
                    Int = jj/4;
                    if floor(Int) == Int
                        % Controllo ampolle
                        if str2double(DatiTiltLinkH(ii,jj)) > 1000
                            DatiTiltLinkH(ii,jj-6:jj) = DatiTiltLinkH(ii-1,jj-6:jj);
                            ErrTiltLinkH(ii,jj) = 1;
                            wardat = ['Data of Tilt Link H nodes corrected using Raw Data of reference Csv file. '...
                                'MEMS was read as an electrolytic cell'];
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiTiltLinkH(nnn,jj)));
                    if check == 1
                        DatiTiltLinkH(nnn,jj) = DatiTiltLinkH(nnn-1,jj);
                        ErrTiltLinkH(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
            corrdat = ['' num2str(Ncorr) ' corrections of Tilt Link H data done!'];
            fprintf(fileID,fmt,corrdat);
            % Concateno informazioni generali e/o letture dai nodi.
            DatiTiltLinkH = [DATiniTLH str2double(DatiTiltLinkH)];
            % Elimino eventuali date doppie
            a = 2;
            while a <= r
                if DatiTiltLinkH(a,1) == DatiTiltLinkH(a-1,1)
                    DatiTiltLinkH(a,:) = [];
                    r = r-1;
                else
                    a = a+1;
                end
            end
        end
    end
    if NuovoZeroTLH == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiTiltLinkH(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiTiltLinkH(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiTiltLinkH(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiTiltLinkH(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiTiltLinkH;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiTiltLinkH;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end  

%%  Tilt Link HR H
if  yesTLHRH == 0 % Non ci sono Tilt Link HR H
    DatiTiltLinkHRH = [];
    ErrTiltLinkHRH = [];
    wardat = 'lettura function: there are not Tilt Link HR H';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-TLHRH-RawData.csv'];
    NAN = 0;
    while NAN == 0
        if datenum(datainiTLHRH) == datenum(Data(1,1))
            tempoiniTLHRH = char(Data(1,2));
        end
        NodeType = 'Tilt Link HR H';
        NodeNumTLHRH = num2str(cell2mat(NodoTiltLinkHRH(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiTLHRH ''' and Time >= ''' tempoiniTLHRH ''' and  ToolNameID = ''' DTcatena...
            ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumTLHRH ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniTLHRH = curs.Data;
        Ntempo = DATiniTLHRH;
        [r,c] = size(DATiniTLHRH);
        saltoTLHRH = 0;
        if  r==1 && c==1
            textdatTLHRH = 'Warning: error reading Tilt Link HR H (control unit data)!';
            fprintf(fileID,fmt,textdatTLHRH);
            saltoTLHRH = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiTLHRH ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
            ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumTLHRH ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2TLHRH = curs.Data;
        Ndate = DAT2TLHRH;
        if saltoTLHRH == 1
            DATiniTLHRH = DAT2TLHRH;
        end
        Check = size(DAT2TLHRH);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoTLHRH == 0
                DATiniTLHRH = [DATiniTLHRH; DAT2TLHRH];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniTLHRH);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniTLHRH(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniTLHRH(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniTLHRH = T;
        else
            DATiniTLHRH = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Tunnel Link
        if saltoTLHRH == 0
            if Check(1,2) > 1 % ho superato il primo giorno
                DatiTiltLinkHRH = cell(rd+rt,6*rTLHRH);
            else
                DatiTiltLinkHRH = cell(rt,6*rTLHRH);
            end
        else
            DatiTiltLinkHRH = cell(rd,6*rTLHRH);
        end
        NTiltLinkHRH = cell2mat(NodoTiltLinkHRH(:,2));
        col = 6;
        ii = 1;
        w = 0;
        while ii <= rTLHRH
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NTiltLinkHRH(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val3, Val4, Val5, Val6 from RawDataView where Date = ''' ...
                datainiTLHRH ''' and Time >= ''' tempoiniTLHRH ''' and UnitName = ''' IDcentralina ''' and  ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoTLHRH = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0, Val1, Val3, Val4, Val5, Val6 from RawDataView where Date > ''' ...
                datainiTLHRH ''' and ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina...
                ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoTLHRHd = curs.Data;
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            ini_col = col-5;
            if saltoTLHRH == 0
                DatiTiltLinkHRH(1:rt,ini_col:col) = DATnodoTLHRH(:,:);
            else
                DatiTiltLinkHRH(1:rd,ini_col:col) = DATnodoTLHRHd(:,:);
            end
            
            [rn,~] = size(DATnodoTLHRHd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoTLHRHd(i,:) = DATnodoTLHRHd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Tilt Link HR H ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoTLHRH == 0
                    DatiTiltLinkHRH(rt+1:rt+rd,ini_col:col) = DATnodoTLHRHd(:,:);
                end
            else
                DatiTiltLinkHRH = DatiTiltLinkHRH(1:end,:);
            end
            okdatTLHRH = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rTLHRH) ' Tilt Link HR H nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatTLHRH);
            ii = ii+1;
            col = col+6;
        end
        if w == 0
            wardat = 'There are not warning for Tilt Link HR H!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiTiltLinkHRH);
        ErrTiltLinkHRH = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroTLHRH == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiTLHRH));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiTiltLinkHRH(1,jj)));
            idxND = find(strcmp('No Data',DatiTiltLinkHRH(1,jj)));
            idxT = find(strcmp('---',DatiTiltLinkHRH(1,jj)));
            idxT2 = find(strcmp('-',DatiTiltLinkHRH(1,jj)));
            idxE2 = find(strcmp('Err2',DatiTiltLinkHRH(1,jj)));
            idxE255 = find(strcmp('Err255',DatiTiltLinkHRH(1,jj)));
            idxD = find(strcmp('DMUXe',DatiTiltLinkHRH(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiTiltLinkHRH(1,jj)));
            idxN = find(strcmp('null',DatiTiltLinkHRH(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of Tilt Link HR H nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroTLHRH == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiTLHRH)+1 < now
                        datainiTLHRH = datestr(datenum(datainiTLHRH)+1,'yyyy-mm-dd');
                        break
                    else
                        yesTLHRH = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiTLHRH = datestr(datenum(datainiTLHRH)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiTiltLinkHRH(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrTiltLinkHRH(1,jj) = 1;
                            wardat = 'Data of Tilt Link HR H nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiTLHRH = datestr(datenum(datainiTLHRH)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
            if ESCI == 0
                % Cerco i multipli di 6 per identificare le temperature
                Int = jj/6;
                if floor(Int) == Int
                    % Controllo ampolle
                    if str2double(DatiTiltLinkHRH(1,jj)) > 1000
                        if rDR==1 && cDR==1 || NuovoZeroTLHRH == 0
                            NAN = 0;
                            ESCI = 1;
                            if datenum(datainiTLHRH)+1 < now
                                datainiTLHRH = datestr(datenum(datainiTLHRH)+1,'yyyy-mm-dd');
                                break
                            else
                                yesTLHRH = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiTLHRH = datestr(datenum(datainiTLHRH)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiTiltLinkHRH(1,jj-5) = cellstr(num2str(DatiRaw(RawDate(end),jj-4)));
                                DatiTiltLinkHRH(1,jj-4) = cellstr(num2str(DatiRaw(RawDate(end),jj-3)));
                                DatiTiltLinkHRH(1,jj-3) = cellstr(num2str(DatiRaw(RawDate(end),jj-2)));
                                DatiTiltLinkHRH(1,jj-2) = cellstr(num2str(DatiRaw(RawDate(end),jj-1)));
                                DatiTiltLinkHRH(1,jj-1) = cellstr(num2str(DatiRaw(RawDate(end),jj)));
                                DatiTiltLinkHRH(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrTiltLinkHRH(1,jj) = 1;
                                wardat = ['Data of Tilt Link HR H nodes corrected using Raw Data of reference Csv file. '...
                                    'MEMS was read as an electrolytic cell'];
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiTiltLinkHRH(:,jj)));
                idxND = find(strcmp('No Data',DatiTiltLinkHRH(1,jj)));
                idxT = find(strcmp('---',DatiTiltLinkHRH(:,jj)));
                idxT2 = find(strcmp('-',DatiTiltLinkHRH(:,jj)));
                idxE2 = find(strcmp('Err2',DatiTiltLinkHRH(:,jj)));
                idxE255 = find(strcmp('Err255',DatiTiltLinkHRH(:,jj)));
                idxD = find(strcmp('DMUXe',DatiTiltLinkHRH(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiTiltLinkHRH(:,jj)));
                idxN = find(strcmp('null',DatiTiltLinkHRH(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiTLHRH = datestr(datenum(datainiTLHRH)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiTiltLinkHRH(indice,jj) = DatiTiltLinkHRH(indice-1,jj);
                    ErrTiltLinkHRH(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiTiltLinkHRH(nnn,jj)));
                    if check == 1
                        DatiTiltLinkHRH(nnn,jj) = DatiTiltLinkHRH(nnn-1,jj);
                        ErrTiltLinkHRH(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
            corrdat = ['' num2str(Ncorr) ' corrections of Tilt Link HR H data done!'];
            fprintf(fileID,fmt,corrdat);
            % Concateno informazioni generali e/o letture dai nodi.
            DatiTiltLinkHRH = [DATiniTLHRH str2double(DatiTiltLinkHRH)];
            % Elimino eventuali date doppie
            a = 2;
            while a <= r
                if DatiTiltLinkHRH(a,1) == DatiTiltLinkHRH(a-1,1)
                    DatiTiltLinkHRH(a,:) = [];
                    r = r-1;
                else
                    a = a+1;
                end
            end
        end
    end
    if NuovoZeroTLHRH == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiTiltLinkHRH(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiTiltLinkHRH(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiTiltLinkHRH(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiTiltLinkHRH(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiTiltLinkHRH;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiTiltLinkHRH;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end  

%%  PreConv Link
if  yesPCL == 0 % Non ci sono PreConv Link
    DatiPreConvLink = [];
    ErrPreConvLink = [];
    wardat = 'lettura function: there are not PreConv Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-PCL-RawData.csv'];
    NAN = 0;
    while NAN == 0
        if datenum(datainiPCL) == datenum(Data(1,1))
            tempoiniPCL = char(Data(1,2));
        end
        NodeType = 'PreConv Link';
        NodeNumPCL = num2str(cell2mat(NodoPreConvLink(rPCL,2)));
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiPCL ''' and Time >= ''' tempoiniPCL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumPCL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniPCL = curs.Data;
        Ntempo = DATiniPCL;
        [r,c] = size(DATiniPCL);
        saltoPCL = 0;
        if  r==1 && c==1
            textdatPCL = 'Warning: error reading PreConv Link (control unit data)!';
            fprintf(fileID,fmt,textdatPCL);
            saltoPCL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiPCL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumPCL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
        DAT2PCL = curs.Data;
        Ndate = DAT2PCL;
        if saltoPCL == 1
            DATiniPCL = DAT2PCL;
        end
        Check = size(DAT2PCL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoPCL == 0
                DATiniPCL = [DATiniPCL; DAT2PCL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniPCL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniPCL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniPCL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniPCL = T;
        else
            DATiniPCL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate);
        [rt,~] = size(Ntempo);
        if saltoPCL == 0
            % creo la matrice per i datiPreConvLink
            if Check(1,2) > 1
                DatiPreConvLink = cell(rd+rt,4*rPCL);
            else
                DatiPreConvLink = cell(rt,4*rPCL);
            end
        else
            DatiPreConvLink = cell(rd,4*rPCL);
        end
        NPreConvLink = cell2mat(NodoPreConvLink(:,2));
        col = 4;
        ii = 1;
        w = 0;
        while ii <= rPCL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NPreConvLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date = ''' ...
                datainiPCL ''' and Time >= ''' tempoiniPCL ''' and UnitName = ''' IDcentralina ...
                ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoPCL = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0, Val1, Val2, Val6 from RawDataView where Date > ''' ...
                datainiPCL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoPCLd = curs.Data;
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            ini_col = col-3;
            if saltoPCL == 0
                DatiPreConvLink(1:rt,ini_col:col) = DATnodoPCL(:,:);
            else
                DatiPreConvLink(1:rd,ini_col:col) = DATnodoPCL(:,:);
            end
            
            [rn,~] = size(DATnodoPCLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoPCLd(i,:) = DATnodoPCLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node PreConv Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoPCL == 0
                    DatiPreConvLink(rt+1:rt+rd,ini_col:col) = DATnodoPCLd(:,:);
                end
            else
                DatiPreConvLink = DatiPreConvLink(1:end,:);
            end
            okdatPCL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rPCL) ' PreConv Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatPCL);
            col = col+4;
            ii = ii+1;
        end
        if w == 0
            wardat = 'There are not warning for PreConv Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiPreConvLink);
        ErrPreConvLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroPCL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiPCL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiPreConvLink(1,jj)));
            idxND = find(strcmp('No Data',DatiPreConvLink(1,jj)));
            idxT = find(strcmp('---',DatiPreConvLink(1,jj)));
            idxT2 = find(strcmp('-',DatiPreConvLink(1,jj)));
            idxE2 = find(strcmp('Err2',DatiPreConvLink(1,jj)));
            idxE255 = find(strcmp('Err255',DatiPreConvLink(1,jj)));
            idxD = find(strcmp('DMUXe',DatiPreConvLink(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiPreConvLink(1,jj)));
            idxN = find(strcmp('null',DatiPreConvLink(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of Pre Conv Link nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroPCL == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiPCL)+1 < now
                        datainiPCL = datestr(datenum(datainiPCL)+1,'yyyy-mm-dd');
                        break
                    else
                        yesPCL = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiPCL = datestr(datenum(datainiPCL)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiPreConvLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrPreConvLink(1,jj) = 1;
                            wardat = 'Data of PreConv Link nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiPCL = datestr(datenum(datainiPCL)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
            if ESCI == 0
                % Cerco i multipli di 4 per identificare le temperature
                Int = jj/4;
                if floor(Int) == Int
                    % Controllo ampolle
                    if str2double(DatiPreConvLink(1,jj)) > 1000
                        if rDR==1 && cDR==1 || NuovoZeroPCL == 0
                            NAN = 0;
                            ESCI = 1;
                            if datenum(datainiPCL)+1 < now
                                datainiPCL = datestr(datenum(datainiPCL)+1,'yyyy-mm-dd');
                                break
                            else
                                yesPCL = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiPCL = datestr(datenum(datainiPCL)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiPreConvLink(1,jj-3) = cellstr(num2str(DatiRaw(RawDate(end),jj-2)));
                                DatiPreConvLink(1,jj-2) = cellstr(num2str(DatiRaw(RawDate(end),jj-1)));
                                DatiPreConvLink(1,jj-1) = cellstr(num2str(DatiRaw(RawDate(end),jj)));
                                DatiPreConvLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrPreConvLink(1,jj) = 1;
                                wardat = ['Data of PreConv Link nodes corrected using Raw Data of reference Csv file. '...
                                    'MEMS was read as an electrolytic cell'];
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiPreConvLink(:,jj)));
                idxND = find(strcmp('No Data',DatiPreConvLink(:,jj)));
                idxT = find(strcmp('---',DatiPreConvLink(:,jj)));
                idxT2 = find(strcmp('-',DatiPreConvLink(:,jj)));
                idxE2 = find(strcmp('Err2',DatiPreConvLink(:,jj)));
                idxE255 = find(strcmp('Err255',DatiPreConvLink(:,jj)));
                idxD = find(strcmp('DMUXe',DatiPreConvLink(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiPreConvLink(:,jj)));
                idxN = find(strcmp('null',DatiPreConvLink(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiPCL = datestr(datenum(datainiPCL)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiPreConvLink(indice,jj) = DatiPreConvLink(indice-1,jj);
                    ErrPreConvLink(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                for ii = 2:r
                    % Cerco i multipli di 4 per identificare le temperature
                    Int = jj/4;
                    if floor(Int) == Int
                        % Controllo ampolle
                        if str2double(DatiPreConvLink(ii,jj)) > 1000
                            DatiPreConvLink(ii,jj-6:jj) = DatiPreConvLink(ii-1,jj-6:jj);
                            ErrPreConvLink(ii,jj) = 1;
                            wardat = ['Data of PreConv Link nodes corrected using Raw Data of reference Csv file. '...
                                'MEMS was read as an electrolytic cell'];
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiPreConvLink(nnn,jj)));
                    if check == 1
                        DatiPreConvLink(nnn,jj) = DatiPreConvLink(nnn-1,jj);
                        ErrPreConvLink(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
        end
        corrdat = ['' num2str(Ncorr) ' corrections of PreConv Link data done!'];
        fprintf(fileID,fmt,corrdat);
        % Concateno informazioni generali e/o letture dai nodi.
        DatiPreConvLink = [DATiniPCL str2double(DatiPreConvLink)];
        % Elimino eventuali date doppie
        a = 2;
        while a <= r
            if DatiPreConvLink(a,1) == DatiPreConvLink(a-1,1)
               DatiPreConvLink(a,:) = [];
               r = r-1;
            else
                a = a+1;
            end
        end
    end
    if NuovoZeroPCL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiPreConvLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiPreConvLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiPreConvLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiPreConvLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiPreConvLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiPreConvLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end

%%  PreConv Link HR  
if  yesPCLHR == 0 % non ci sono PreConvLinkHR
    DatiPreConvLinkHR = [];
    ErrPreConvLinkHR = [];
    wardat = 'lettura function: there are not PreConv Link HR';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-PCLHR-RawData.csv'];
    NAN = 0;
    while NAN == 0
        if datenum(datainiPCLHR) == datenum(Data(1,1))
            tempoiniPCLHR = char(Data(1,2));
        end
        NodeType = 'PreConv Link HR';
        NodeNumPCLHR = num2str(cell2mat(NodoPreConvLinkHR(rPCLHR,2)));
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiPCLHR ''' and Time >= ''' tempoiniPCLHR ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumPCLHR ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniPCLHR = curs.Data;
        Ntempo = DATiniPCLHR;
        [r,c] = size(DATiniPCLHR);
        saltoPCLHR = 0;
        if  r==1 && c==1
            textdatPCLHR = 'Warning: error reading PreConv Link HR (control unit data)!';
            fprintf(fileID,fmt,textdatPCLHR);
            saltoPCLHR = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiPCLHR ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' ...
            NodeNumPCLHR ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2PCLHR = curs.Data;
        Ndate = DAT2PCLHR;
        if saltoPCLHR == 1
            DATiniPCLHR = DAT2PCLHR;
        end
        % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
        Check = size(DAT2PCLHR);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoPCLHR == 1
                DATiniPCLHR = [DATiniPCLHR; DAT2PCLHR];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniPCLHR);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniPCLHR(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniPCLHR(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniPCLHR = T;
        else
            DATiniPCLHR = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate);
        [rt,~] = size(Ntempo);
        % creo la matrice per i dati
        if saltoPCLHR == 0
            if Check(1,2) > 1
                DatiPreConvLinkHR = cell(rd+rt,3*rPCLHR);
            else
                DatiPreConvLinkHR = cell(rt,3*rPCLHR);
            end
        else
            DatiPreConvLinkHR = cell(rd,3*rPCLHR);
        end
        NPreConvLinkHR = cell2mat(NodoPreConvLinkHR(:,2));
        ii = 1;
        col = 3;
        w = 0;
        while ii <= rPCL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NPreConvLinkHR(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val3, Val4, Val5, Val6 from RawDataView where Date = ''' datainiPCLHR ''' and Time >= ''' ...
                tempoiniPCLHR ''' and UnitName = ''' IDcentralina ''' and  ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoPCLHR = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0, Val1, Val3, Val4, Val5, Val6 from RawDataView where Date > ''' ...
                datainiPCLHR ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoPCLHRd = curs.Data;
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            ini_col = col-5;
            if saltoPCLHR == 0
                DatiPreConvLinkHR(1:rt,ini_col:col) = DATnodoPCLHR(:,:);
            else
                DatiPreConvLinkHR(1:rd,ini_col:col) = DATnodoPCLHR(:,:);
            end
            
            [rn,~] = size(DATnodoTLHRdH); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoPCLHRd(i,:) = DATnodoPCLHRd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node PreConv Link HR ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoPCLHR == 0
                    DatiPreConvLinkHR(rt+1:rt+rd,ini_col:col) = DATnodoPCLHRd(:,:);
                end
            else
                DatiPreConvLinkHR = DatiPreConvLinkHR(1:end,:);
            end
            okdatPCLHR = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rPCLHR) ' PreConv Link HR nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatPCLHR);
            col = col+3;
            ii = ii+1;
        end
        if w == 0
            wardat = 'There are not warning for PreConv Link HR!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiPreConvLinkHR);
        ErrPreConvLinkHR = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroPCLHR == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiPCLHR));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiPreConvLinkHR(1,jj)));
            idxND = find(strcmp('No Data',DatiPreConvLinkHR(1,jj)));
            idxT = find(strcmp('---',DatiPreConvLinkHR(1,jj)));
            idxT2 = find(strcmp('-',DatiPreConvLinkHR(1,jj)));
            idxE2 = find(strcmp('Err2',DatiPreConvLinkHR(1,jj)));
            idxE255 = find(strcmp('Err255',DatiPreConvLinkHR(1,jj)));
            idxD = find(strcmp('DMUXe',DatiPreConvLinkHR(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiPreConvLinkHR(1,jj)));
            idxN = find(strcmp('null',DatiPreConvLinkHR(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of PreConv Link HR nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroPCLHR == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiPCLHR)+1 < now
                        datainiPCLHR = datestr(datenum(datainiPCLHR)+1,'yyyy-mm-dd');
                        break
                    else
                        yesPCLHR = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiPCLHR = datestr(datenum(datainiPCLHR)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiPreConvLinkHR(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrPreConvLinkHR(1,jj) = 1;
                            wardat = 'Data of PreConv Link HR nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiPCLHR = datestr(datenum(datainiPCLHR)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
            if ESCI == 0
                % Cerco i multipli di 6 per identificare le temperature
                Int = jj/6;
                if floor(Int) == Int
                    % Controllo ampolle
                    if str2double(DatiPreConvLinkHR(1,jj)) < 500
                        if rDR==1 && cDR==1 || NuovoZeroPCLHR == 0
                            NAN = 0;
                            ESCI = 1;
                            if datenum(datainiPCLHR)+1 < now
                                datainiPCLHR = datestr(datenum(datainiPCLHR)+1,'yyyy-mm-dd');
                                break
                            else
                                yesPCLHR = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiPCLHR = datestr(datenum(datainiPCLHR)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiPreConvLinkHR(1,jj-5) = cellstr(num2str(DatiRaw(RawDate(end),jj-4)));
                                DatiPreConvLinkHR(1,jj-4) = cellstr(num2str(DatiRaw(RawDate(end),jj-3)));
                                DatiPreConvLinkHR(1,jj-3) = cellstr(num2str(DatiRaw(RawDate(end),jj-2)));
                                DatiPreConvLinkHR(1,jj-2) = cellstr(num2str(DatiRaw(RawDate(end),jj-1)));
                                DatiPreConvLinkHR(1,jj-1) = cellstr(num2str(DatiRaw(RawDate(end),jj)));
                                DatiPreConvLinkHR(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrPreConvLinkHR(1,jj) = 1;
                                wardat = ['Data of PreConv Link HR nodes corrected using Raw Data of reference Csv file. '...
                                    'MEMS was read as an electrolytic cell'];
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiPreConvLinkHR(:,jj)));
                idxND = find(strcmp('No Data',DatiPreConvLinkHR(:,jj)));
                idxT = find(strcmp('---',DatiPreConvLinkHR(:,jj)));
                idxT2 = find(strcmp('-',DatiPreConvLinkHR(:,jj)));
                idxE2 = find(strcmp('Err2',DatiPreConvLinkHR(:,jj)));
                idxE255 = find(strcmp('Err255',DatiPreConvLinkHR(:,jj)));
                idxD = find(strcmp('DMUXe',DatiPreConvLinkHR(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiPreConvLinkHR(:,jj)));
                idxN = find(strcmp('null',DatiPreConvLinkHR(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiPCLHR = datestr(datenum(datainiPCLHR)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiPreConvLinkHR(indice,jj) = DatiPreConvLinkHR(indice-1,jj);
                    ErrPreConvLinkHR(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiPreConvLinkHR(nnn,jj)));
                    if check == 1
                        DatiPreConvLinkHR(nnn,jj) = DatiPreConvLinkHR(nnn-1,jj);
                        ErrPreConvLinkHR(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
            corrdat = ['' num2str(Ncorr) ' corrections of PreConv Link HR data done!'];
            fprintf(fileID,fmt,corrdat);
            % Concateno informazioni generali e/o letture dai nodi.
            DatiPreConvLinkHR = [DATiniPCLHR str2double(DatiPreConvLinkHR)];
            % Elimino eventuali date doppie
            a = 2;
            while a <= r
                if DatiPreConvLinkHR(a,1) == DatiPreConvLinkHR(a-1,1)
                    DatiPreConvLinkHR(a,:) = [];
                    r = r-1;
                else
                    a = a+1;
                end
            end
        end
    end
    if NuovoZeroPCLHR == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiPreConvLinkHR(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiPreConvLinkHR(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiPreConvLinkHR(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiPreConvLinkHR(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiPreConvLinkHR;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiPreConvLinkHR;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end

%%  DistoMT Link
if  yesDM == 0 % Non ci sono DistoMT Link
    DatiDistoMTLink = [];
    ErrDistoMTLink = [];
    wardat = 'lettura function: there are not DistoMT Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-DM-RawData.csv'];
    NAN = 0;
    while NAN == 0
        if datenum(datainiDM) == datenum(Data(1,1))
            tempoiniDM = char(Data(1,2));
        end
        NodeType = 'DistoMT Link';
        NodeNumDM = num2str(cell2mat(NodoDistoMTLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiDM ''' and Time >= ''' tempoiniDM ''' and  ToolNameID = ''' DTcatena...
            ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumDM ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniDM = curs.Data;
        Ntempo = DATiniDM;
        [r,c] = size(DATiniDM);
        saltoDM = 0;
        if  r==1 && c==1
            textdatDM = 'Warning: error reading DistoMT Link (control unit data)!';
            fprintf(fileID,fmt,textdatDM);
            saltoDM = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiDM ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumDM ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2DM = curs.Data;
        Ndate = DAT2DM;
        if saltoDM == 1
            DATiniDM = DAT2DM;
        end
        Check = size(DAT2DM);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoDM == 0
                DATiniDM = [DATiniDM; DAT2DM];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniDM);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniDM(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniDM(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniDM = T;
        else
            DATiniDM = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Tunnel Link
        if saltoDM == 0
            if Check(1,2) > 1 % ho superato il primo giorno
                DatiDistoMTLink = cell(rd+rt,4*rDM);
            else
                DatiDistoMTLink = cell(rt,4*rDM);
            end
        else
            DatiDistoMTLink = cell(rd,4*rDM);
        end
        NDistoMTLink = cell2mat(NodoDistoMTLink(:,2));
        col = 4;
        ii = 1;
        w = 0;
        while ii <= rDM
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NDistoMTLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val2, Val3 from RawDataView where Date = ''' ...
                datainiDM ''' and Time >= ''' tempoiniDM ''' and UnitName = ''' IDcentralina...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoDM = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0, Val1, Val2, Val3 from RawDataView where Date > ''' ...
                datainiDM ''' and UnitName = ''' IDcentralina ...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoDMd = curs.Data;
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            ini_col = col-3;
            if saltoDM == 0
                DatiDistoMTLink(1:rt,ini_col:col) = DATnodoDM(:,:);
            else
                DatiDistoMTLink(1:rd,ini_col:col) = DATnodoDMd(:,:);
            end
            
            [rn,~] = size(DATnodoDMd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoDMd(i,:) = DATnodoDMd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node DistoMT Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoDM == 0
                    DatiDistoMTLink(rt+1:rt+rd,ini_col:col) = DATnodoDMd(:,:);
                end
            else
                DatiDistoMTLink = DatiDistoMTLink(1:end,:);
            end
            okdatDM = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rDM) ' DistoMT Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatDM);
            ii = ii+1;
            col = col+4;
        end
        if w == 0
            wardat = 'There are not warning for DistoMT Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiDistoMTLink);
        ErrDistoMTLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroDM == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiDM));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        % --- Codice per il primo dato ---
        for jj=1:c
            % Trovo l'indice della cella contenenti una determinata stringa.
            idxE = find(strcmp('Err1',DatiDistoMTLink(1,jj)));
            idxND = find(strcmp('No Data',DatiDistoMTLink(1,jj)));
            idxT = find(strcmp('---',DatiDistoMTLink(1,jj)));
            idxT2 = find(strcmp('-',DatiDistoMTLink(1,jj)));
            idxE2 = find(strcmp('Err2',DatiDistoMTLink(1,jj)));
            idxE255 = find(strcmp('Err255',DatiDistoMTLink(1,jj)));
            idxD = find(strcmp('DMUXe',DatiDistoMTLink(1,jj)));
            idxE1F = find(strcmp('Err1File',DatiDistoMTLink(1,jj)));
            idxN = find(strcmp('null',DatiDistoMTLink(1,jj)));
            idx = union(idxE,idxT);
            idx = union(idx,idxND);
            idx = union(idx,idxT2);
            idx = union(idx,idxE2);
            idx = union(idx,idxE255);
            idx = union(idx,idxD);
            idx = union(idx,idxE1F);
            idx = union(idx,idxN);  
            [ri,~] = size(idx);
            if ri == 1
                wardat = 'Warning on the correction of DistoMT Link nodes: there are not data at the beginning of the array!';
                fprintf(fileID,fmt,wardat);
                if rDR==1 && cDR==1 || NuovoZeroDM == 0 % il file non è mai stato scritto, cerco la data precedente
                    NAN = 0;
                    ESCI = 1;
                    if datenum(datainiDM)+1 < now
                        datainiDM = datestr(datenum(datainiDM)+1,'yyyy-mm-dd');
                        break
                    else
                        yesDM = 0;
                    end
                else
                    if isempty(RawDate) == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiDM = datestr(datenum(datainiDM)-1,'yyyy-mm-dd');
                        break
                    else
                        if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                            DatiDistoMTLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                            ErrDistoMTLink(1,jj) = 1;
                            wardat = 'Data of DistoMT Link nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        else
                            NAN = 0;
                            ESCI = 1;
                            datainiDM = datestr(datenum(datainiDM)-1,'yyyy-mm-dd');
                            break
                        end
                    end
                end
            end
        end
        if ESCI == 0
            for jj=1:c % nodi
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiDistoMTLink(:,jj)));
                idxND = find(strcmp('No Data',DatiDistoMTLink(:,jj)));
                idxT = find(strcmp('---',DatiDistoMTLink(:,jj)));
                idxT2 = find(strcmp('-',DatiDistoMTLink(:,jj)));
                idxE2 = find(strcmp('Err2',DatiDistoMTLink(:,jj)));
                idxE255 = find(strcmp('Err255',DatiDistoMTLink(:,jj)));
                idxD = find(strcmp('DMUXe',DatiDistoMTLink(:,jj)));
                idxE1F = find(strcmp('Err1File',DatiDistoMTLink(:,jj)));
                idxN = find(strcmp('null',DatiDistoMTLink(:,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                [ri,~] = size(idx);
                for kk=1:ri
                    indice=idx(kk);
                    if indice == 1
                        NAN = 0;
                        ESCI = 1;
                        datainiDM = datestr(datenum(datainiDM)-1,'yyyy-mm-dd');
                        break
                    end
                    DatiDistoMTLink(indice,jj) = DatiDistoMTLink(indice-1,jj);
                    ErrDistoMTLink(indice,jj) = 1;
                    Ncorr = Ncorr+ri;
                    NAN = 1;
                end
                if ESCI == 1
                    break
                end
                % Elimino gli eventuali Not a Number
                for nnn = 2:r % date
                    check = isnan(cell2mat(DatiDistoMTLink(nnn,jj)));
                    if check == 1
                        DatiDistoMTLink(nnn,jj) = DatiDistoMTLink(nnn-1,jj);
                        ErrDistoMTLink(nnn,jj) = 1;
                        Ncorr = Ncorr+ri;
                    end
                end
            end
            corrdat = ['' num2str(Ncorr) ' corrections of DistoMT Link data done!'];
            fprintf(fileID,fmt,corrdat);
            % Concateno informazioni generali e/o letture dai nodi.
            DatiDistoMTLink = [DATiniDM str2double(DatiDistoMTLink)];
            % Elimino eventuali date doppie
            a = 2;
            while a <= r
                if DatiDistoMTLink(a,1) == DatiDistoMTLink(a-1,1)
                    DatiDistoMTLink(a,:) = [];
                    r = r-1;
                else
                    a = a+1;
                end
            end
        end
    end
    if NuovoZeroDM == 1
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiDistoMTLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiDistoMTLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiDistoMTLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiDistoMTLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiDistoMTLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiDistoMTLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end  

%%  Pressure Link
if  yesPL == 0 % Non ci sono Pressure Link
    DatiPressureLink = [];
    ErrPressureLink = [];
    wardat = 'lettura function: there are not Pressure Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-Pressure-RawData.csv']; % Specifico per Pressure Link
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(datainiPL) == datenum(Data(1,1))
            tempoiniPL = char(Data(1,2));
        end
        NodeType = 'Pressure Link';
        NodeNumPL = num2str(cell2mat(NodoPressureLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiPL ''' and Time >= ''' tempoiniPL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumPL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniPL = curs.Data;
        Ntempo = DATiniPL;
        [r,c] = size(DATiniPL);
        saltoPL = 0;
        if  r==1 && c==1
            textdatPL = 'Warning: error reading Pressure Link (control unit data)!';
            fprintf(fileID,fmt,textdatPL);
            saltoPL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiPL ''' and UnitName = ''' IDcentralina ...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
            NodeType ''' and NodeNum = ''' NodeNumPL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2PL = curs.Data;
        Ndate = DAT2PL;
        if saltoPL == 1
            DATiniPL = DAT2PL;
        end
        Check = size(DAT2PL);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoPL == 0
                DATiniPL = [DATiniPL; DAT2PL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniPL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniPL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniPL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniPL = T;
        else
            DATiniPL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        if saltoPL == 0
            if Check(1,2) > 1
                % creo la matrice per i dati Pressure Link
                DatiPressureLink = cell(rd+rt,rPrL);
            else
                DatiPressureLink = cell(rt,rPrL);
            end
        else
            DatiPressureLink = cell(rd,rPrL);
        end
        NPressureLink = cell2mat(NodoPressureLink(:,2));
        col = 1;
        ii = 1;
        w = 0;
        while ii <= rPrL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NPressureLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datainiPL ''' and Time >= ''' tempoiniPL ''' and UnitName = ''' IDcentralina ...
                ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoPL = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0 from RawDataView where Date > ''' ...
                datainiPL ''' and UnitName = ''' IDcentralina ...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoPLd = curs.Data;
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoPL == 0
                DatiPressureLink(1:rt,col) = DATnodoPL(:,:);
            else
                DatiPressureLink(1:rd,col) = DATnodoPL(:,:);
            end
            
            [rn,~] = size(DATnodoPLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoPLd(i,:) = DATnodoPLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Pressure Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoPL == 0
                    DatiPressureLink(rt+1:rt+rd,col) = DATnodoPLd(:,:);
                end
            else
                DatiPressureLink = DatiPressureLink(1:end,:);
            end
            okdatPL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rPrL) ' Pressure Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatPL);
            ii = ii+1;
            col = col+1;
        end
        if w == 0
            wardat = 'There are not warning for Pressure Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiPressureLink);
        ErrPressureLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroPrL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiPL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
            % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',DatiPressureLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',DatiPressureLink(dis,jj)) == 0
                       DatiPressureLink = DatiPressureLink(dis:end,:);
                       DATiniPL = DATiniPL(dis:end,:);
                       [r,c] = size(DatiPressureLink);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(datainiPL)+1 < now
                            datainiPL = datestr(datenum(datainiPL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesPL = 0;
                        end
                    end
                end                
            end
        end
        if DIS ~= 1 && yesPL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiPressureLink(1,jj)));
                idxT = find(strcmp('---',DatiPressureLink(1,jj)));
                idxT2 = find(strcmp('-',DatiPressureLink(1,jj)));
                idxE2 = find(strcmp('Err2',DatiPressureLink(1,jj)));
                idxE255 = find(strcmp('Err255',DatiPressureLink(1,jj)));
                idxD = find(strcmp('DMUXe',DatiPressureLink(1,jj)));
                idxE1F = find(strcmp('Err1File',DatiPressureLink(1,jj)));
                idxN = find(strcmp('null',DatiPressureLink(1,jj)));
                idxRX = find(strcmp('No RX',DatiPressureLink(1,jj)));
                idxI = find(strcmp('id Error',DatiPressureLink(1,jj)));
                idxM = find(strcmp('MUX id Error',DatiPressureLink(1,jj)));
                idxC = find(strcmp('CH n. Error',DatiPressureLink(1,jj)));
                idxU = find(strcmp('Unknown Error',DatiPressureLink(1,jj)));
                idxMC = find(strcmp('MsgCorrup',DatiPressureLink(1,jj)));
                idxNS = find(strcmp('NotSupp',DatiPressureLink(1,jj)));
                idxU2 = find(strcmp('Unknown',DatiPressureLink(1,jj)));
                idxNA = find(strcmp('NotAv',DatiPressureLink(1,jj)));
                idxND = find(strcmp('No Data',DatiPressureLink(1,jj)));
                idxCE = find(strcmp('CH n. Er',DatiPressureLink(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxND);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of Pressure Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZeroPrL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(datainiPL)+1 < now
                            datainiPL = datestr(datenum(datainiPL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesPL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiPL = datestr(datenum(datainiPL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                DatiPressureLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrPressureLink(1,jj) = 1;
                                wardat = 'Data of Pressure Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                datainiPL = datestr(datenum(datainiPL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiPressureLink(:,jj)));
                    idxT = find(strcmp('---',DatiPressureLink(:,jj)));
                    idxT2 = find(strcmp('-',DatiPressureLink(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiPressureLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiPressureLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiPressureLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiPressureLink(:,jj)));
                    idxN = find(strcmp('null',DatiPressureLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiPressureLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiPressureLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiPressureLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiPressureLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiPressureLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiPressureLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiPressureLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiPressureLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiPressureLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiPressureLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiPressureLink(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiPL = datestr(datenum(datainiPL)-1,'yyyy-mm-dd');
                            break
                        else
                            DatiPressureLink(indice,jj) = DatiPressureLink(indice-1,jj);
                            ErrPressureLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiPressureLink(nnn,jj)));
                        if check == 1
                            DatiPressureLink(nnn,jj) = DatiPressureLink(nnn-1,jj);
                            ErrPressureLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of Pressure Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiPressureLink = [DATiniPL str2double(DatiPressureLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiPressureLink(a,1) == DatiPressureLink(a-1,1)
                        DatiPressureLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
    end
    if NuovoZeroPrL == 1 && yesPL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiPressureLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiPressureLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiPressureLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiPressureLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiPressureLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiPressureLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end

%%  Load Link
if  yesLL == 0 % Non ci sono Load Link
    DatiLoadLink = [];
    ErrLoadLink = [];
    wardat = 'lettura function: there are not Load Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-Load-RawData.csv']; % Specifico per Load Link
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(datainiLL) == datenum(Data(1,1))
            tempoiniLL = char(Data(1,2));
        end
        NodeType = 'Load Link';
        NodeNumLL = num2str(cell2mat(NodoLoadLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiLL ''' and Time >= ''' tempoiniLL ''' and UnitName = ''' IDcentralina ...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumLL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniLL = curs.Data;
        Ntempo = DATiniLL;
        [r,c] = size(DATiniLL);
        saltoLL = 0;
        if r==1 && c==1
            textdatLL = 'Warning: error reading Load Link (control unit data)!';
            fprintf(fileID,fmt,textdatLL);
            saltoLL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiLL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumLL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2LL = curs.Data;
        Ndate = DAT2LL;
        if saltoLL == 1
            DATiniLL = DAT2LL;
        end
        Check = size(DAT2LL);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoLL == 0
                DATiniLL = [DATiniLL; DAT2LL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniLL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniLL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniLL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniLL = T;
        else
            DATiniLL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Load Link
        if saltoLL == 0
            if Check(1,2) > 1
                DatiLoadLink = cell(rd+rt,rLL);
            else
                DatiLoadLink = cell(rt,rLL);
            end
        else
            DatiLoadLink = cell(rd,rLL);
        end
        NLoadLink = cell2mat(NodoLoadLink(:,2));
        col = 1;
        ii = 1;
        w = 0;
        while ii <= rLL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NLoadLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datainiLL ''' and Time >= ''' tempoiniLL ''' and UnitName = ''' IDcentralina ...
                ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0 from RawDataView where Date > ''' ...
                datainiLL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLLd = curs.Data;
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoLL == 0
                DatiLoadLink(1:rt,col) = DATnodoLL(:,:);
            else
                DatiLoadLink(1:rd,col) = DATnodoLL(:,:);
            end
            
            [rn,~] = size(DATnodoLLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoLLd(i,:) = DATnodoLLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Load Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoLL == 0
                    DatiLoadLink(rt+1:rt+rd,col) = DATnodoLLd(:,:);
                end
            else
                DatiLoadLink = DatiLoadLink(1:end,:);
            end
            okdatLL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rLL) ' Load Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatLL);
            ii = ii+1;
            col = col+1;
        end
        if w == 0
            wardat = 'There are not warning for Load Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiLoadLink);
        ErrLoadLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroLL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiLL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
            if strcmp('Dis.',DatiLoadLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',DatiLoadLink(dis,jj)) == 0
                       DatiLoadLink = DatiLoadLink(dis:end,:);
                       DATiniLL = DATiniLL(dis:end,:);
                       [r,c] = size(DatiLoadLink);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(datainiLL)+1 < now
                            datainiLL = datestr(datenum(datainiLL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesLL = 0;
                        end
                    end
                end
            end
        end
        if DIS ~= 1 && yesLL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiLoadLink(1,jj)));
                idxT = find(strcmp('---',DatiLoadLink(1,jj)));
                idxT2 = find(strcmp('-',DatiLoadLink(1,jj)));
                idxE2 = find(strcmp('Err2',DatiLoadLink(1,jj)));
                idxE255 = find(strcmp('Err255',DatiLoadLink(1,jj)));
                idxD = find(strcmp('DMUXe',DatiLoadLink(1,jj)));
                idxE1F = find(strcmp('Err1File',DatiLoadLink(1,jj)));
                idxN = find(strcmp('null',DatiLoadLink(1,jj)));
                idxRX = find(strcmp('No RX',DatiLoadLink(1,jj)));
                idxI = find(strcmp('id Error',DatiLoadLink(1,jj)));
                idxM = find(strcmp('MUX id Error',DatiLoadLink(1,jj)));
                idxC = find(strcmp('CH n. Error',DatiLoadLink(1,jj)));
                idxU = find(strcmp('Unknown Error',DatiLoadLink(1,jj)));
                idxMC = find(strcmp('MsgCorrup',DatiLoadLink(1,jj)));
                idxNS = find(strcmp('NotSupp',DatiLoadLink(1,jj)));
                idxU2 = find(strcmp('Unknown',DatiLoadLink(1,jj)));
                idxNA = find(strcmp('NotAv',DatiLoadLink(1,jj)));
                idxND = find(strcmp('No Data',DatiLoadLink(1,jj)));
                idxCE = find(strcmp('CH n. Er',DatiLoadLink(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxND);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of Load Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZeroLL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(datainiLL)+1 < now
                            datainiLL = datestr(datenum(datainiLL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesLL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiLL = datestr(datenum(datainiLL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                DatiLoadLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrLoadLink(1,jj) = 1;
                                wardat = 'Data of Load Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                datainiLL = datestr(datenum(datainiLL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiLoadLink(:,jj)));
                    idxT = find(strcmp('---',DatiLoadLink(:,jj)));
                    idxT2 = find(strcmp('-',DatiLoadLink(1,jj)));
                    idxE2 = find(strcmp('Err2',DatiLoadLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiLoadLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiLoadLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiLoadLink(:,jj)));
                    idxN = find(strcmp('null',DatiLoadLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiLoadLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiLoadLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiLoadLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiLoadLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiLoadLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiLoadLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiLoadLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiLoadLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiLoadLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiLoadLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiLoadLink(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiLL = datestr(datenum(datainiLL)-1,'yyyy-mm-dd');
                            break
                        else
                            DatiLoadLink(indice,jj) = DatiLoadLink(indice-1,jj);
                            ErrLoadLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiLoadLink(nnn,jj)));
                        if check == 1
                            DatiLoadLink(nnn,jj) = DatiLoadLink(nnn-1,jj);
                            ErrLoadLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of Load Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiLoadLink = [DATiniLL str2double(DatiLoadLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiLoadLink(a,1) == DatiLoadLink(a-1,1)
                        DatiLoadLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
    end
    if NuovoZeroLL == 1 && yesLL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiLoadLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiLoadLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiLoadLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiLoadLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiLoadLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiLoadLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end

%%  Extensometer Link
if  yesEL == 0 % Non ci sono Extensometer Link
    DatiExtensometerLink = [];
    DatiExtensometerLink_Rif = [];
    DatiNTCExtensometerLink = [];
    ErrExtensometerLink = [];
    ChEL = [];
    EL_NTC = [];
    wardat = 'lettura function: there are not Extensometer Link';
    fprintf(fileID,fmt,wardat);
else
    NAN = 0;
    DIS = 0;
    ChEL = cell2mat(NodoExtensometerLink(:,4));
    EL_NTC = cell2mat(NodoExtensometerLink(:,5));
    RawDataFile = ['' IDcentralina '-' DTcatena '-Extensometer-RawData.csv']; % Specifico per Extensometer Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-ExtensometerNTC-RawData.csv']; % Specifico per NTC Extensometer Link
    while NAN == 0
        if datenum(datainiEL) == datenum(Data(1,1))
            tempoiniEL = char(Data(1,2));
        end
        NodeType = 'Extensometer Link';
        NodeNumEL = num2str(cell2mat(NodoExtensometerLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiEL ''' and Time >= ''' tempoiniEL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniEL = curs.Data;
        Ntempo = DATiniEL;
        [r,c] = size(DATiniEL);
        saltoEL = 0;
        if r==1 && c==1
            textdatEL = 'Warning: error reading Extensometer Link (control unit data)!';
            fprintf(fileID,fmt,textdatEL);
            saltoEL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiEL ''' and UnitName = ''' IDcentralina ...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2EL = curs.Data;
        Ndate = DAT2EL;
        if saltoEL == 1
            DATiniEL = DAT2EL;
        end
        Check = size(DAT2EL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoEL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATiniEL = [DATiniEL; DAT2EL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniEL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniEL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniEL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniEL = T;
        else
            DATiniEL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Extensometer Link
        col = 1;
        ii = 1;
        if saltoEL == 0
            if Check(1,2) > 1
                DatiExtensometerLink = cell(rd+rt,rEL);
                DatiNTCExtensometerLink = cell(rd+rt,rEL);
            else
                DatiExtensometerLink = cell(rt,rEL);
                DatiNTCExtensometerLink = cell(rt,rEL);
            end
        else
            DatiExtensometerLink = cell(rd,rEL);
            DatiNTCExtensometerLink = cell(rd,rEL);
        end
        DatiExtensometerLink_Rif = cell(150,rEL);
        NExtensometerLink = cell2mat(NodoExtensometerLink(:,2));
        w = 0;
        while ii <= rEL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NExtensometerLink(ii,1);
            nNodo = num2str(nN);
            % Dato
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datainiEL ''' and Time >= ''' tempoiniEL ''' and  ToolNameID = ''' ...
                DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoEL = curs.Data;
            % Dato
            comando = ['select Val0 from RawDataView where Date > ''' ...
                datainiEL ''' and UnitName = ''' IDcentralina ...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoELd = curs.Data;
            % Riferimento
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datestr(Date_Rif_EL,'yyyy-mm-dd') ''' and Time >= ''' datestr(Date_Rif_EL,'HH:MM:SS')...
                ''' and  ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoEL_Rif = curs.Data;
            if ChEL(ii,1) == 1
                % T
                DATnodoEL_NTC = 0;
                DATnodoELd_NTC = 0;
            elseif ChEL(ii,1) >= 2
                    if EL_NTC(ii,1)==1
                        % Temperatura
                        comando = ['select Val1 from RawDataView where Date = ''' ...
                            datainiEL ''' and Time >= ''' tempoiniEL ''' and UnitName = ''' IDcentralina...
                            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                            NodeType ''' and NodeNum ='  nNodo ''];
                        curs = exec(conn,comando);
                        curs = fetch(curs);
                        DATnodoEL_NTC = curs.Data;
                        comando = ['select Val1 from RawDataView where Date > ''' ...
                            datainiEL ''' and UnitName = ''' IDcentralina ...
                            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                            NodeType ''' and NodeNum = '  nNodo ''];
                        curs = exec(conn,comando);
                        curs = fetch(curs);
                        DATnodoELd_NTC = curs.Data;
                    else
                        DATnodoEL_NTC = 0;
                        DATnodoELd_NTC = 0;
                    end
            end
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoEL == 0
                if ChEL(ii,1) >= 2
                    if EL_NTC(ii,1)==1
                        DatiExtensometerLink(1:rt,col) = DATnodoEL(:,:);
                        DatiNTCExtensometerLink(1:rt,col) = DATnodoEL_NTC(:,:);
                    else
                        DatiExtensometerLink(1:rt,col) = DATnodoEL(:,:); %#ok<*AGROW>
                    end
                elseif ChEL(ii,1) == 1
                    DatiExtensometerLink(1:rt,col) = DATnodoEL(:,:); %#ok<*AGROW>
                end
            else
                if ChEL(ii,1) >= 2
                    if EL_NTC(ii,1)==1
                        DatiExtensometerLink(1:rd,col) = DATnodoEL(:,:);
                        DatiNTCExtensometerLink(1:rd,col) = DATnodoEL_NTC(:,:);
                    else
                        DatiExtensometerLink(1:rd,col) = DATnodoEL(:,:); %#ok<*AGROW>
                    end
                elseif ChEL(ii,1) == 1
                    DatiExtensometerLink(1:rd,col) = DATnodoEL(:,:); %#ok<*AGROW>
                end
            end
            
            % Riferimento
            [dim,~] = size(DATnodoEL_Rif); % controllo date
            DatiExtensometerLink_Rif(1:dim,col) = DATnodoEL_Rif;
            if col == 1
                DatiExtensometerLink_Rif(dim+1:end,:) = [];
            end
            
            [rn,~] = size(DATnodoELd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoELd(i,:) = DATnodoELd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Extensometer Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            [rnT,~] = size(DATnodoELd_NTC); % controllo date
            if rnT < rd % normalmente sono uguali
                for i = rnT+1:rd
                    DATnodoELd_NTC(i,:) = DATnodoELd_NTC(rnT,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node Extensometer Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoEL == 0
                    if ChEL(ii,1) == 1
                        DatiExtensometerLink(rt+1:rt+rd,col) = DATnodoELd(:,:);
                    elseif EL_NTC(ii,1) == 0
                           DatiExtensometerLink(rt+1:rt+rd,col) = DATnodoELd(:,:);
                    else
                        DatiExtensometerLink(rt+1:rt+rd,col) = DATnodoELd(:,:);
                        DatiNTCExtensometerLink(rt+1:rt+rd,col) = DATnodoELd_NTC(:,:);
                    end
                end
            else
                DatiExtensometerLink = DatiExtensometerLink(1:end,:);
                DatiNTCExtensometerLink = DatiNTCExtensometerLink(1:end,:);
            end
            okdatEL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rEL) ' Extensometer Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatEL);
            ii = ii+1;
            col = col+1;
        end
        if w == 0
            wardat = 'There are not warning for Extensometer Link!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo       
        % Dati
        [r,c] = size(DatiExtensometerLink);
        ErrExtensometerLink = zeros(r,c);
        Ncorr = 0;
        NAN = 1;
        ESCI = 0;
        RIF = 0;
        row = 1;
        if NuovoZeroEL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiEL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        FileRif = ['' IDcentralina '-' DTcatena '-ExtensometerRif.csv'];
        while RIF == 0
            if row <= dim
                % Controllo la data di riferimento
                idxE = find(strcmp('Err1',DatiExtensometerLink_Rif(row,:)));
                idxT = find(strcmp('---',DatiExtensometerLink_Rif(row,:)));
                idxT2 = find(strcmp('-',DatiExtensometerLink_Rif(row,:)));
                idxE2 = find(strcmp('Err2',DatiExtensometerLink_Rif(row,:)));
                idxE255 = find(strcmp('Err255',DatiExtensometerLink_Rif(row,:)));
                idxD = find(strcmp('DMUXe',DatiExtensometerLink_Rif(row,:)));
                idxE1F = find(strcmp('Err1File',DatiExtensometerLink_Rif(row,:)));
                idxN = find(strcmp('null',DatiExtensometerLink_Rif(row,:)));
                idxRX = find(strcmp('No RX',DatiExtensometerLink_Rif(row,:)));
                idxI = find(strcmp('id Error',DatiExtensometerLink_Rif(row,:)));
                idxM = find(strcmp('MUX id Error',DatiExtensometerLink_Rif(row,:)));
                idxC = find(strcmp('CH n. Error',DatiExtensometerLink_Rif(row,:)));
                idxU = find(strcmp('Unknown Error',DatiExtensometerLink_Rif(row,:)));
                idxMC = find(strcmp('MsgCorrup',DatiExtensometerLink_Rif(row,:)));
                idxNS = find(strcmp('NotSupp',DatiExtensometerLink_Rif(row,:)));
                idxU2 = find(strcmp('Unknown',DatiExtensometerLink_Rif(row,:)));
                idxNA = find(strcmp('NotAv',DatiExtensometerLink_Rif(row,:)));
                idxDIS = find(strcmp('Dis.',DatiExtensometerLink_Rif(row,:)));
                idxND = find(strcmp('No Data',DatiExtensometerLink_Rif(row,:)));
                idxCE = find(strcmp('CH n. Er',DatiExtensometerLink_Rif(row,:)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxDIS);
                idx = union(idx,idxND);
                [~,ri] = size(idx);
                if ri >= 1 && isempty(idx) == 0 % Se almeno uno è in errore, allora guardo la riga dopo
                    if isfile(FileRif) == 1
                        DatiRif = csvread(FileRif);
                        if isnan(DatiRif(1,ri)) == 0
                            DatiExtensometerLink_Rif(1,ri) = cellstr(num2str(DatiRif(1,ri)));
                        else
                            row = row+1;
                        end
                    else
                        row = row+1;
                    end
                else
                    RIF = 1;
                    DatiExtensometerLink_Rif = DatiExtensometerLink_Rif(row,:);
                end
            else
                Date_Rif_EL = datestr(datenum(Date_Rif_EL)+1,'yyyy-mm-dd');
                RIF = 1;
                NAN = 0;
            end
        end
        if NAN == 1
            for jj=1:c
                % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
                % è ancora stato installato!)
                if strcmp('Dis.',DatiExtensometerLink(1,jj)) == 1
                    for dis = 2:r
                        if strcmp('Dis.',DatiExtensometerLink(dis,jj)) == 0
                            DatiExtensometerLink = DatiExtensometerLink(dis:end,:);
                            DATiniEL = DATiniEL(dis:end,:);
                            DatiNTCExtensometerLink = DatiNTCExtensometerLink(dis:end,:);
                            [r,c] = size(DatiExtensometerLink);
                            break
                        elseif dis == r
                            NAN = 0;
                            DIS = 1;
                            if datenum(datainiEL)+1 < now
                                datainiEL = datestr(datenum(datainiEL)+1,'yyyy-mm-dd');
                                break
                            else
                                yesEL = 0;
                            end
                        end
                    end
                end
            end
        
            if DIS ~= 1 && yesEL == 1
                % --- Codice per il primo dato ---
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiExtensometerLink(1,jj)));
                    idxT = find(strcmp('---',DatiExtensometerLink(1,jj)));
                    idxT2 = find(strcmp('-',DatiExtensometerLink(1,jj)));
                    idxE2 = find(strcmp('Err2',DatiExtensometerLink(1,jj)));
                    idxE255 = find(strcmp('Err255',DatiExtensometerLink(1,jj)));
                    idxD = find(strcmp('DMUXe',DatiExtensometerLink(1,jj)));
                    idxE1F = find(strcmp('Err1File',DatiExtensometerLink(1,jj)));
                    idxN = find(strcmp('null',DatiExtensometerLink(1,jj)));
                    idxRX = find(strcmp('No RX',DatiExtensometerLink(1,jj)));
                    idxI = find(strcmp('id Error',DatiExtensometerLink(1,jj)));
                    idxM = find(strcmp('MUX id Error',DatiExtensometerLink(1,jj)));
                    idxC = find(strcmp('CH n. Error',DatiExtensometerLink(1,jj)));
                    idxU = find(strcmp('Unknown Error',DatiExtensometerLink(1,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiExtensometerLink(1,jj)));
                    idxNS = find(strcmp('NotSupp',DatiExtensometerLink(1,jj)));
                    idxU2 = find(strcmp('Unknown',DatiExtensometerLink(1,jj)));
                    idxNA = find(strcmp('NotAv',DatiExtensometerLink(1,jj)));
                    idxND = find(strcmp('No Data',DatiExtensometerLink(1,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiExtensometerLink(1,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    if ri == 1
                        wardat = 'Warning on the correction of Extensometer Link nodes: there are not data at the beginning of the array!';
                        fprintf(fileID,fmt,wardat);
                        if rDR==1 && cDR==1 || NuovoZeroEL == 0 % il file non è mai stato scritto, cerco la data precedente
                            NAN = 0;
                            ESCI = 1;
                            if datenum(datainiEL)+1 < now
                                datainiEL = datestr(datenum(datainiEL)+1,'yyyy-mm-dd');
                                break
                            else
                                yesEL = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiEL = datestr(datenum(datainiEL)-1,'yyyy-mm-dd');
                                break
                            else
                                if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                    DatiExtensometerLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                    ErrExtensometerLink(1,jj) = 1;
                                    wardat = 'Data of Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                else
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiEL = datestr(datenum(datainiEL)-1,'yyyy-mm-dd');
                                    break
                                end
                            end
                        end
                    end
                end
                if ESCI == 0
                    for jj=1:c
                        idxE = find(strcmp('Err1',DatiExtensometerLink(:,jj)));
                        idxT = find(strcmp('---',DatiExtensometerLink(:,jj)));
                        idxT2 = find(strcmp('-',DatiExtensometerLink(:,jj)));
                        idxE2 = find(strcmp('Err2',DatiExtensometerLink(:,jj)));
                        idxE255 = find(strcmp('Err255',DatiExtensometerLink(:,jj)));
                        idxD = find(strcmp('DMUXe',DatiExtensometerLink(:,jj)));
                        idxE1F = find(strcmp('Err1File',DatiExtensometerLink(:,jj)));
                        idxN = find(strcmp('null',DatiExtensometerLink(:,jj)));
                        idxRX = find(strcmp('No RX',DatiExtensometerLink(:,jj)));
                        idxI = find(strcmp('id Error',DatiExtensometerLink(:,jj)));
                        idxM = find(strcmp('MUX id Error',DatiExtensometerLink(:,jj)));
                        idxC = find(strcmp('CH n. Error',DatiExtensometerLink(:,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiExtensometerLink(:,jj)));
                        idxU = find(strcmp('Unknown Error',DatiExtensometerLink(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiExtensometerLink(:,jj)));
                        idxNS = find(strcmp('NotSupp',DatiExtensometerLink(:,jj)));
                        idxU2 = find(strcmp('Unknown',DatiExtensometerLink(:,jj)));
                        idxNA = find(strcmp('NotAv',DatiExtensometerLink(:,jj)));
                        idxND = find(strcmp('No Data',DatiExtensometerLink(:,jj)));
                        idx = union(idxND,idxE);
                        idx = union(idx,idxT);
                        idx = union(idx,idxT2);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxND);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiEL = datestr(datenum(datainiEL)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiExtensometerLink(indice,jj) = DatiExtensometerLink(indice-1,jj);
                                ErrExtensometerLink(indice,jj) = 1;
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(DatiExtensometerLink(nnn,jj)));
                            if check == 1
                                DatiExtensometerLink(nnn,jj) = DatiExtensometerLink(nnn-1,jj);
                                ErrExtensometerLink(nnn,jj) = 1;
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of Extensometer Link data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    DatiExtensometerLink = [DATiniEL str2double(DatiExtensometerLink)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if DatiExtensometerLink(a,1) == DatiExtensometerLink(a-1,1)
                            DatiExtensometerLink(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            end
            if ESCI == 0
                if sum(EL_NTC(:,1))>0
                    if DIS ~= 1 && yesEL == 1
                        % T
                        [r,c] = size(DatiNTCExtensometerLink);
                        Ncorr = 0;
                        if NuovoZeroEL == 1
                            if isfile(RawDataFile) == 1
                                DatiRawNTC = csvread(RawDataFileNTC);
                                [rDR,cDR] = size(DatiRawNTC);
                                DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                                RawDateNTC = find(DatiRawNTC(:,1)<=datenum(datainiEL));
                            else
                                rDR = 1;
                                cDR = 1;
                                RawDateNTC = [];
                                DatiRawNTC = [];
                            end
                        else
                            rDR = 1;
                            cDR = 1;
                            RawDateNTC = [];
                            DatiRawNTC = [];
                        end
                        for jj=1:c
                            % Trovo l'indice della cella contenenti una determinata stringa.
                            idxE = find(strcmp('Err1',DatiNTCExtensometerLink(1,jj)));
                            idxT = find(strcmp('---',DatiNTCExtensometerLink(1,jj)));
                            idxT2 = find(strcmp('-',DatiNTCExtensometerLink(1,jj)));
                            idxE2 = find(strcmp('Err2',DatiNTCExtensometerLink(1,jj)));
                            idxE255 = find(strcmp('Err255',DatiNTCExtensometerLink(1,jj)));
                            idxD = find(strcmp('DMUXe',DatiNTCExtensometerLink(1,jj)));
                            idxE1F = find(strcmp('Err1File',DatiNTCExtensometerLink(1,jj)));
                            idxN = find(strcmp('null',DatiNTCExtensometerLink(1,jj)));
                            idxRX = find(strcmp('No RX',DatiNTCExtensometerLink(1,jj)));
                            idxI = find(strcmp('id Error',DatiNTCExtensometerLink(1,jj)));
                            idxM = find(strcmp('MUX id Error',DatiNTCExtensometerLink(1,jj)));
                            idxC = find(strcmp('CH n. Error',DatiNTCExtensometerLink(1,jj)));
                            idxU = find(strcmp('Unknown Error',DatiNTCExtensometerLink(1,jj)));
                            idxMC = find(strcmp('MsgCorrup',DatiNTCExtensometerLink(1,jj)));
                            idxNS = find(strcmp('NotSupp',DatiNTCExtensometerLink(1,jj)));
                            idxU2 = find(strcmp('Unknown',DatiNTCExtensometerLink(1,jj)));
                            idxNA = find(strcmp('NotAv',DatiNTCExtensometerLink(1,jj)));
                            idxND = find(strcmp('No Data',DatiNTCExtensometerLink(1,jj)));
                            idxCE = find(strcmp('CH n. Er',DatiNTCExtensometerLink(1,jj)));
                            idx = union(idxE,idxT);
                            idx = union(idx,idxT2);
                            idx = union(idx,idxCE);
                            idx = union(idx,idxE2);
                            idx = union(idx,idxE255);
                            idx = union(idx,idxD);
                            idx = union(idx,idxE1F);
                            idx = union(idx,idxN);
                            idx = union(idx,idxRX);
                            idx = union(idx,idxI);
                            idx = union(idx,idxM);
                            idx = union(idx,idxC);
                            idx = union(idx,idxU);
                            idx = union(idx,idxMC);
                            idx = union(idx,idxNS);
                            idx = union(idx,idxU2);
                            idx = union(idx,idxNA);
                            idx = union(idx,idxND);
                            [ri,~] = size(idx);
                            if ri == 1
                                wardat = 'Warning on the correction of NTC of Extensometer Link nodes: there are not data at the beginning of the array!';
                                fprintf(fileID,fmt,wardat);
                                if rDR==1 && cDR==1 || NuovoZeroEL == 0 % il file non è mai stato scritto, cerco la data precedente
                                    NAN = 0;
                                    ESCI = 1;
                                    if datenum(datainiEL)+1 < now
                                        datainiEL = datestr(datenum(datainiEL)+1,'yyyy-mm-dd');
                                        break
                                    else
                                        yesEL = 0;
                                    end
                                else
                                    if isempty(RawDateNTC) == 1
                                        NAN = 0;
                                        ESCI = 1;
                                        datainiEL = datestr(datenum(datainiEL)-1,'yyyy-mm-dd');
                                        break
                                    else
                                        DatiNTCExtensometerLink(1,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                        if ErrExtensometerLink(1,jj) == 0
                                            ErrExtensometerLink(1,jj) = 0.5;
                                        else
                                            ErrExtensometerLink(1,jj) = 1;
                                        end
                                        wardat = 'Data of NTC of Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                        fprintf(fileID,fmt,wardat);
                                        Ncorr = Ncorr+ri;
                                        NAN = 1;
                                    end
                                end
                            end
                        end
                        for jj=1:c
                            % Trovo l'indice della cella contenenti una determinata stringa.
                            idxE = find(strcmp('Err1',DatiNTCExtensometerLink(:,jj)));
                            idxT = find(strcmp('-',DatiNTCExtensometerLink(:,jj)));
                            idxT3 = find(strcmp('---',DatiNTCExtensometerLink(:,jj)));
                            idxE2 = find(strcmp('Err2',DatiNTCExtensometerLink(:,jj)));
                            idxE255 = find(strcmp('Err255',DatiNTCExtensometerLink(:,jj)));
                            idxD = find(strcmp('DMUXe',DatiNTCExtensometerLink(:,jj)));
                            idxE1F = find(strcmp('Err1File',DatiNTCExtensometerLink(:,jj)));
                            idxN = find(strcmp('null',DatiNTCExtensometerLink(:,jj)));
                            idxRX = find(strcmp('No RX',DatiNTCExtensometerLink(:,jj)));
                            idxI = find(strcmp('id Error',DatiNTCExtensometerLink(:,jj)));
                            idxM = find(strcmp('MUX id Error',DatiNTCExtensometerLink(:,jj)));
                            idxC = find(strcmp('CH n. Error',DatiNTCExtensometerLink(:,jj)));
                            idxU = find(strcmp('Unknown Error',DatiNTCExtensometerLink(:,jj)));
                            idxMC = find(strcmp('MsgCorrup',DatiNTCExtensometerLink(:,jj)));
                            idxNS = find(strcmp('NotSupp',DatiNTCExtensometerLink(:,jj)));
                            idxU2 = find(strcmp('Unknown',DatiNTCExtensometerLink(:,jj)));
                            idxNA = find(strcmp('NotAv',DatiNTCExtensometerLink(:,jj)));
                            idxND = find(strcmp('No Data',DatiNTCExtensometerLink(:,jj)));
                            idxCE = find(strcmp('CH n. Er',DatiNTCExtensometerLink(:,jj)));
                            idx = union(idxE,idxT);
                            idx = union(idx,idxT3);
                            idx = union(idx,idxCE);
                            idx = union(idx,idxE2);
                            idx = union(idx,idxE255);
                            idx = union(idx,idxD);
                            idx = union(idx,idxE1F);
                            idx = union(idx,idxN);
                            idx = union(idx,idxRX);
                            idx = union(idx,idxI);
                            idx = union(idx,idxM);
                            idx = union(idx,idxC);
                            idx = union(idx,idxU);
                            idx = union(idx,idxMC);
                            idx = union(idx,idxNS);
                            idx = union(idx,idxU2);
                            idx = union(idx,idxNA);
                            idx = union(idx,idxND);
                            [ri,~] = size(idx);
                            for kk=1:ri
                                indice=idx(kk);
                                if indice == 1
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiEL = datestr(datenum(datainiEL)-1,'yyyy-mm-dd');
                                    break
                                else
                                    DatiNTCExtensometerLink(indice,jj) = DatiNTCExtensometerLink(indice-1,jj);
                                    if ErrExtensometerLink(indice,jj) == 0
                                        ErrExtensometerLink(indice,jj) = 0.5;
                                    else
                                        ErrExtensometerLink(indice,jj) = 1;
                                    end
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                end
                            end
                            if ESCI == 1
                                break
                            end
                            % Elimino gli eventuali Not a Number
                            for nnn = 2:r
                                check = isnan(cell2mat(DatiNTCExtensometerLink(nnn,jj)));
                                if check == 1
                                    DatiNTCExtensometerLink(nnn,jj) = DatiNTCExtensometerLink(nnn-1,jj);
                                    if ErrExtensometerLink(indice,jj) == 0
                                        ErrExtensometerLink(indice,jj) = 0.5;
                                    else
                                        ErrExtensometerLink(indice,jj) = 1;
                                    end
                                    Ncorr = Ncorr+ri;
                                end
                            end
                        end
                        corrdat = ['' num2str(Ncorr) ' corrections of NTC for Extensometer Link data done!'];
                        fprintf(fileID,fmt,corrdat);
                        % Concateno informazioni generali e/o letture dai nodi.
                        DatiNTCExtensometerLink = [DATiniEL str2double(DatiNTCExtensometerLink)];
                        % Elimino eventuali date doppie
                        a = 2;
                        while a <= r
                            if DatiNTCExtensometerLink(a,1) == DatiNTCExtensometerLink(a-1,1)
                                DatiNTCExtensometerLink(a,:) = [];
                                r = r-1;
                            else
                                a = a+1;
                            end
                        end
                    end
                end
            end
        end
    end
    if NuovoZeroEL == 1 && yesEL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiExtensometerLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiExtensometerLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiExtensometerLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiExtensometerLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiExtensometerLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
        
        if sum(EL_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTCExtensometerLink(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTCExtensometerLink(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTCExtensometerLink(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTCExtensometerLink(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTCExtensometerLink;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = DatiExtensometerLink;
        if sum(EL_NTC(:,1))>0
            DatiNTC = DatiNTCExtensometerLink;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(EL_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
    if isfile(FileRif) == 0
        csvwrite(FileRif,str2double(DatiExtensometerLink_Rif));
    end
end

%%  3D Extensometer Link
if  yes3DEL == 0 % Non ci sono 3D Extensometer Link
    Dati3DExtensometerLink = [];
    Dati3DExtensometerLink_Rif = [];
    Err3DExtensometerLink = [];
    DatiNTC3DExtensometerLink = [];
    ErrNTC3DExtensometerLink = [];
    Ch3DEL = [];
    EL3D_NTC = [];
    wardat = 'lettura function: there are not 3D Extensometer Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-3DExtensometer-RawData.csv']; % Specifico per 3D Extensometer Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-3DExtensometerNTC-RawData.csv']; % Specifico per NTC 3D Extensometer Link
    Ch3DEL = cell2mat(Nodo3DExtensometerLink(:,4));
    EL3D_NTC = cell2mat(Nodo3DExtensometerLink(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(dataini3DEL) == datenum(Data(1,1))
            tempoini3DEL = char(Data(1,2));
        end
        NodeType = '3D Extensometer Link';
        NodeNum3DEL = num2str(cell2mat(Nodo3DExtensometerLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            dataini3DEL ''' and Time >= ''' tempoini3DEL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNum3DEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATini3DEL = curs.Data;
        Ntempo = DATini3DEL;
        [r,c] = size(DATini3DEL);
        salto3DEL = 0;
        if r==1 && c==1
            textdat3DEL = 'Warning: error reading 3D Extensometer Link (control unit data)!';
            fprintf(fileID,fmt,textdat3DEL);
            salto3DEL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            dataini3DEL ''' and UnitName = ''' IDcentralina...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNum3DEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT23DEL = curs.Data;
        Ndate = DAT23DEL;
        if salto3DEL == 1
            DATini3DEL = DAT23DEL;
        end
        Check = size(DAT23DEL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if salto3DEL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATini3DEL = [DATini3DEL; DAT23DEL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATini3DEL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATini3DEL(:,1)) repmat(' ', [rD,1]) cell2mat(DATini3DEL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATini3DEL = T;
        else
            DATini3DEL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        if salto3DEL == 0
            % creo la matrice per i dati 3D Extensometer Link
            if Check(1,2) > 1
                Dati3DExtensometerLink = cell(rd+rt,3*r3DEL); 
                DatiNTC3DExtensometerLink = cell(rd+rt,r3DEL); 
            else
                Dati3DExtensometerLink = cell(rt,3*r3DEL);
                DatiNTC3DExtensometerLink = cell(rt,r3DEL);
            end
        else
            Dati3DExtensometerLink = cell(rd,3*r3DEL);
            DatiNTC3DExtensometerLink = cell(rd,r3DEL);
        end
        Dati3DExtensometerLink_Rif = cell(150,3*r3DEL);
        N3DExtensometerLink = cell2mat(Nodo3DExtensometerLink(:,2));
        col = 1;
        colT = 1;
        ii = 1;
        w = 0;
        while ii <= r3DEL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = N3DExtensometerLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0, Val1, Val2 from RawDataView where Date = ''' ...
                dataini3DEL ''' and Time >= ''' tempoini3DEL ''' and UnitName = ''' IDcentralina...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodo3DEL = curs.Data;
            comando = ['select Val0, Val1, Val2 from RawDataView where Date > ''' ...
                dataini3DEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodo3DELd = curs.Data;
            % Riferimento
            comando = ['select Val0, Val1, Val2 from RawDataView where Date = ''' ...
                datestr(Date_Rif_3DEL,'yyyy-mm-dd') ''' and Time >= ''' datestr(Date_Rif_3DEL,'HH:MM:SS')...
                ''' and  ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodo3DEL_Rif = curs.Data;
            if Ch3DEL(ii,1) == 3
                % T
                DATnodo3DEL_NTC = 0;
                DATnodo3DELd_NTC = 0;
            elseif Ch3DEL(ii,1) == 4
                if EL3D_NTC(ii,1)==1
                    comando = ['select Val3 from RawDataView where Date = ''' ...
                        dataini3DEL ''' and Time >= ''' tempoini3DEL ''' and UnitName = ''' IDcentralina ...
                        ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodo3DEL_NTC = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val3 from RawDataView where Date > ''' ...
                        dataini3DEL ''' and UnitName = ''' IDcentralina...
                        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType...
                        ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodo3DELd_NTC = curs.Data;
                else
                    DATnodo3DEL_NTC = 0;
                    DATnodo3DELd_NTC = 0;
                end
            end
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if salto3DEL == 0
                Dati3DExtensometerLink(1:rt,col:col+2) = DATnodo3DEL(:,:);
                if Ch3DEL(ii,1) == 1
                     DatiNTC3DExtensometerLink(1:rt,colT) = DATnodo3DEL_NTC(:,:);
                end
            else
                Dati3DExtensometerLink(1:rd,col:col+2) = DATnodo3DEL(:,:);
                if Ch3DEL(ii,1) == 1
                     DatiNTC3DExtensometerLink(1:rd,colT) = DATnodo3DEL_NTC(:,:);
                end
            end
            
            % Riferimento
            [dim,~] = size(DATnodo3DEL_Rif); % controllo date
            Dati3DExtensometerLink_Rif(1:dim,col:col+2) = DATnodo3DEL_Rif;
            if col == 1
                Dati3DExtensometerLink_Rif(dim+1:end,:) = [];
            end
            
            [rn,~] = size(DATnodo3DELd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodo3DELd(i,:) = DATnodo3DELd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node 3D Extensometer Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            [rnT,~] = size(DATnodo3DELd_NTC); % controllo date
            if rnT < rd % normalmente sono uguali
                for i = rnT+1:rd
                    DATnodo3DELd_NTC(i,:) = DATnodo3DELd_NTC(rnT,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node 3D Extensometer Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if salto3DEL == 0
                    Dati3DExtensometerLink(rt+1:rt+rd,col:col+2) = DATnodo3DELd(:,:);
                    if EL3D_NTC(ii,1) == 1 % Si NTC
                        Dati3DExtensometerLink(rt+1:rt+rd,colT) = DATnodo3DELd_NTC(:,:);
                    end
                end
            else
                Dati3DExtensometerLink = Dati3DExtensometerLink(1:end,:);
                DatiNTC3DExtensometerLink = DatiNTC3DExtensometerLink(1:end,:);
            end
            okdat3DEL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(r3DEL) ' 3D Extensometer Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdat3DEL);
            ii = ii+1;
            col = col+3;
            colT = colT+1;
        end
        if w == 0
            wardat = 'There are not warning for 3D Extensometer Link!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(Dati3DExtensometerLink);
        Err3DExtensometerLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        RIF = 0;
        row = 1;
        if NuovoZero3DEL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(dataini3DEL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        while RIF == 0
            if row <= dim
                % Controllo la data di riferimento
                idxE = find(strcmp('Err1',Dati3DExtensometerLink_Rif(row,:)));
                idxT = find(strcmp('---',Dati3DExtensometerLink_Rif(row,:)));
                idxT2 = find(strcmp('-',Dati3DExtensometerLink_Rif(row,:)));
                idxE2 = find(strcmp('Err2',Dati3DExtensometerLink_Rif(row,:)));
                idxE255 = find(strcmp('Err255',Dati3DExtensometerLink_Rif(row,:)));
                idxD = find(strcmp('DMUXe',Dati3DExtensometerLink_Rif(row,:)));
                idxE1F = find(strcmp('Err1File',Dati3DExtensometerLink_Rif(row,:)));
                idxN = find(strcmp('null',Dati3DExtensometerLink_Rif(row,:)));
                idxRX = find(strcmp('No RX',Dati3DExtensometerLink_Rif(row,:)));
                idxI = find(strcmp('id Error',Dati3DExtensometerLink_Rif(row,:)));
                idxM = find(strcmp('MUX id Error',Dati3DExtensometerLink_Rif(row,:)));
                idxC = find(strcmp('CH n. Error',Dati3DExtensometerLink_Rif(row,:)));
                idxU = find(strcmp('Unknown Error',Dati3DExtensometerLink_Rif(row,:)));
                idxMC = find(strcmp('MsgCorrup',Dati3DExtensometerLink_Rif(row,:)));
                idxNS = find(strcmp('NotSupp',Dati3DExtensometerLink_Rif(row,:)));
                idxU2 = find(strcmp('Unknown',Dati3DExtensometerLink_Rif(row,:)));
                idxNA = find(strcmp('NotAv',Dati3DExtensometerLink_Rif(row,:)));
                idxDIS = find(strcmp('Dis.',Dati3DExtensometerLink_Rif(row,:)));
                idxND = find(strcmp('No Data',Dati3DExtensometerLink_Rif(row,:)));
                idxCE = find(strcmp('CH n. Er',Dati3DExtensometerLink_Rif(row,:)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxDIS);
                idx = union(idx,idxND);
                [~,ri] = size(idx);
                if ri >= 1 && isempty(idx) == 0 % Se almeno uno è in errore, allora guardo la riga dopo
                   row = row+1;
                else
                    RIF = 1;
                    Dati3DExtensometerLink_Rif = Dati3DExtensometerLink_Rif(row,:);
                end
            else
                Date_Rif_3DEL = datestr(datenum(Date_Rif_3DEL)+1,'yyyy-mm-dd');
                RIF = 1;
                NAN = 0;
            end
        end
        
        for jj=1:c
            % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',Dati3DExtensometerLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',Dati3DExtensometerLink(dis,jj)) == 0
                        Dati3DExtensometerLink = Dati3DExtensometerLink(dis:end,:);
                        DatiNTC3DExtensometerLink = DatiNTC3DExtensometerLink(dis:end,:);
                        DATini3DEL = DATini3DEL(dis:end,:);
                        [r,c] = size(Dati3DExtensometerLink);
                        break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(dataini3DEL)+1 < now
                            dataini3DEL = datestr(datenum(dataini3DEL)+1,'yyyy-mm-dd');
                            break
                        else
                            yes3DEL = 0;
                        end
                    end
                end
            end
        end
        if DIS ~= 1 && yes3DEL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                idxE = find(strcmp('Err1',Dati3DExtensometerLink(1,jj)));
                idxT = find(strcmp('---',Dati3DExtensometerLink(1,jj)));
                idxT2 = find(strcmp('-',Dati3DExtensometerLink(1,jj)));
                idxE2 = find(strcmp('Err2',Dati3DExtensometerLink(1,jj)));
                idxE255 = find(strcmp('Err255',Dati3DExtensometerLink(1,jj)));
                idxD = find(strcmp('DMUXe',Dati3DExtensometerLink(1,jj)));
                idxE1F = find(strcmp('Err1File',Dati3DExtensometerLink(1,jj)));
                idxN = find(strcmp('null',Dati3DExtensometerLink(1,jj)));
                idxRX = find(strcmp('No RX',Dati3DExtensometerLink(1,jj)));
                idxI = find(strcmp('id Error',Dati3DExtensometerLink(1,jj)));
                idxM = find(strcmp('MUX id Error',Dati3DExtensometerLink(1,jj)));
                idxC = find(strcmp('CH n. Error',Dati3DExtensometerLink(1,jj)));
                idxU = find(strcmp('Unknown Error',Dati3DExtensometerLink(1,jj)));
                idxMC = find(strcmp('MsgCorrup',Dati3DExtensometerLink(1,jj)));
                idxNS = find(strcmp('NotSupp',Dati3DExtensometerLink(1,jj)));
                idxU2 = find(strcmp('Unknown',Dati3DExtensometerLink(1,jj)));
                idxNA = find(strcmp('NotAv',Dati3DExtensometerLink(1,jj)));
                idxND = find(strcmp('No Data',Dati3DExtensometerLink(1,jj)));
                idxCE = find(strcmp('CH n. Er',Dati3DExtensometerLink(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxND);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of 3D Extensometer Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZero3DEL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(dataini3DEL)+1 < now
                            dataini3DEL = datestr(datenum(dataini3DEL)+1,'yyyy-mm-dd');
                            break
                        else
                            yes3DEL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            dataini3DEL = datestr(datenum(dataini3DEL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                Dati3DExtensometerLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                Err3DExtensometerLink(1,jj) = 1;
                                wardat = 'Data of 3D Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                dataini3DEL = datestr(datenum(dataini3DEL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    idxE = find(strcmp('Err1',Dati3DExtensometerLink(:,jj)));
                    idxT = find(strcmp('---',Dati3DExtensometerLink(:,jj)));
                    idxT2 = find(strcmp('-',Dati3DExtensometerLink(:,jj)));
                    idxE2 = find(strcmp('Err2',Dati3DExtensometerLink(:,jj)));
                    idxE255 = find(strcmp('Err255',Dati3DExtensometerLink(:,jj)));
                    idxD = find(strcmp('DMUXe',Dati3DExtensometerLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',Dati3DExtensometerLink(:,jj)));
                    idxN = find(strcmp('null',Dati3DExtensometerLink(:,jj)));
                    idxRX = find(strcmp('No RX',Dati3DExtensometerLink(:,jj)));
                    idxI = find(strcmp('id Error',Dati3DExtensometerLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',Dati3DExtensometerLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',Dati3DExtensometerLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',Dati3DExtensometerLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',Dati3DExtensometerLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',Dati3DExtensometerLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',Dati3DExtensometerLink(:,jj)));
                    idxNA = find(strcmp('NotAv',Dati3DExtensometerLink(:,jj)));
                    idxND = find(strcmp('No Data',Dati3DExtensometerLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',Dati3DExtensometerLink(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            dataini3DEL = datestr(datenum(dataini3DEL)+1,'yyyy-mm-dd');
                            break
                        else
                            Dati3DExtensometerLink(indice,jj) = Dati3DExtensometerLink(indice-1,jj);
                            Err3DExtensometerLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(Dati3DExtensometerLink(nnn,jj)));
                        if check == 1
                            Dati3DExtensometerLink(nnn,jj) = Dati3DExtensometerLink(nnn-1,jj);
                            Err3DExtensometerLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of 3D Extensometer Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                Dati3DExtensometerLink = [DATini3DEL str2double(Dati3DExtensometerLink)];
                Dati3DExtensometerLink_Rif = str2double(Dati3DExtensometerLink_Rif);
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if Dati3DExtensometerLink(a,1) == Dati3DExtensometerLink(a-1,1)
                        Dati3DExtensometerLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
            
        if ESCI == 0
            if sum(EL3D_NTC(:,1))>0
                if DIS ~= 1 && yes3DEL == 1
                    % T
                    [r,c] = size(DatiNTC3DExtensometerLink);
                    ErrNTC3DExtensometerLink = zeros(r,c);
                    Ncorr = 0;
                    if NuovoZero3DEL == 1
                        if isfile(RawDataFile) == 1
                            DatiRawNTC = csvread(RawDataFileNTC);
                            [rDR,cDR] = size(DatiRawNTC);
                            DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                            RawDateNTC = find(DatiRawNTC(:,1)<=datenum(dataini3DEL));
                        else
                            rDR = 1;
                            cDR = 1;
                            RawDateNTC = [];
                            DatiRawNTC = [];
                        end
                    else
                        rDR = 1;
                        cDR = 1;
                        RawDateNTC = [];
                        DatiRawNTC = [];
                    end
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTC3DExtensometerLink(1,jj)));
                        idxT = find(strcmp('---',DatiNTC3DExtensometerLink(1,jj)));
                        idxT2 = find(strcmp('-',DatiNTC3DExtensometerLink(1,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTC3DExtensometerLink(1,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTC3DExtensometerLink(1,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTC3DExtensometerLink(1,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTC3DExtensometerLink(1,jj)));
                        idxN = find(strcmp('null',DatiNTC3DExtensometerLink(1,jj)));
                        idxRX = find(strcmp('No RX',DatiNTC3DExtensometerLink(1,jj)));
                        idxI = find(strcmp('id Error',DatiNTC3DExtensometerLink(1,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTC3DExtensometerLink(1,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTC3DExtensometerLink(1,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTC3DExtensometerLink(1,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTC3DExtensometerLink(1,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTC3DExtensometerLink(1,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTC3DExtensometerLink(1,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTC3DExtensometerLink(1,jj)));
                        idxND = find(strcmp('No Data',DatiNTC3DExtensometerLink(1,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTC3DExtensometerLink(1,jj)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxT2);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxND);
                        [ri,~] = size(idx);
                        if ri == 1
                            wardat = ['Warning on the correction of NTC of 3D Extensometer Link nodes: there are not data at '...
                                'the beginning of the array!'];
                            fprintf(fileID,fmt,wardat);
                            if rDR==1 && cDR==1 || NuovoZero3DEL == 0 % il file non è mai stato scritto, cerco la data precedente
                                NAN = 0;
                                ESCI = 1;
                                dataini3DEL = datestr(datenum(dataini3DEL)-1,'yyyy-mm-dd');
                                break
                            else
                                if isempty(RawDateNTC) == 1
                                    NAN = 0;
                                    ESCI = 1;
                                    if datenum(dataini3DEL)+1 < now
                                        dataini3DEL = datestr(datenum(dataini3DEL)+1,'yyyy-mm-dd');
                                        break
                                    else
                                        yes3DEL = 0;
                                    end
                                else
                                    DatiNTC3DExtensometerLink(1,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                    if Err3DExtensometerLink(1,jj) == 0
                                        Err3DExtensometerLink(1,jj) = 0.5;
                                    else
                                        Err3DExtensometerLink(1,jj) = 1;
                                    end
                                    wardat = 'Data of NTC of 3D Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                end
                            end
                        end
                    end
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTC3DExtensometerLink(:,jj)));
                        idxT = find(strcmp('-',DatiNTC3DExtensometerLink(:,jj)));
                        idxT3 = find(strcmp('---',DatiNTC3DExtensometerLink(:,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTC3DExtensometerLink(:,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTC3DExtensometerLink(:,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTC3DExtensometerLink(:,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTC3DExtensometerLink(:,jj)));
                        idxN = find(strcmp('null',DatiNTC3DExtensometerLink(:,jj)));
                        idxRX = find(strcmp('No RX',DatiNTC3DExtensometerLink(:,jj)));
                        idxI = find(strcmp('id Error',DatiNTC3DExtensometerLink(:,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTC3DExtensometerLink(:,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTC3DExtensometerLink(:,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTC3DExtensometerLink(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTC3DExtensometerLink(:,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTC3DExtensometerLink(:,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTC3DExtensometerLink(:,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTC3DExtensometerLink(:,jj)));
                        idxND = find(strcmp('No Data',DatiNTC3DExtensometerLink(:,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTC3DExtensometerLink(:,jj)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxND);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxT3);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini3DEL = datestr(datenum(dataini3DEL)+1,'yyyy-mm-dd');
                            else
                                DatiNTC3DExtensometerLink(indice,jj) = DatiNTC3DExtensometerLink(indice-1,jj);
                                if Err3DExtensometerLink(indice,jj) == 0
                                    ErrNTC3DExtensometerLink(indice,jj) = 0.5;
                                else
                                    ErrNTC3DExtensometerLink(indice,jj) = 1;
                                end
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(DatiNTC3DExtensometerLink(nnn,jj)));
                            if check == 1
                                DatiNTC3DExtensometerLink(nnn,jj) = DatiNTC3DExtensometerLink(nnn-1,jj);
                                ErrNTC3DExtensometerLink(nnn,jj) = 1;
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of NTC for 3D Extensometer Link data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    DatiNTC3DExtensometerLink = [DATini3DEL str2double(DatiNTC3DExtensometerLink)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if DatiNTC3DExtensometerLink(a,1) == DatiNTC3DExtensometerLink(a-1,1)
                            DatiNTC3DExtensometerLink(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            else
                ErrNTC3DExtensometerLink = 0;
            end
        end
    end
    if NuovoZero3DEL == 1 && yes3DEL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=Dati3DExtensometerLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(Dati3DExtensometerLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(Dati3DExtensometerLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);Dati3DExtensometerLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = Dati3DExtensometerLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end

        if sum(EL3D_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTC3DExtensometerLink(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTC3DExtensometerLink(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTC3DExtensometerLink(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTC3DExtensometerLink(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTC3DExtensometerLink;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = Dati3DExtensometerLink;
        if sum(EL3D_NTC(:,1))>0
            DatiNTC = DatiNTC3DExtensometerLink;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(EL3D_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
end

%%  Wire Extensometer Link
if  yesWEL == 0 % Non ci sono Wire Extensometer Link
    DatiWireExtensometerLink = [];
    DatiNTCWireExtensometerLink = [];
    ErrWireExtensometerLink = [];
    ChWEL = [];
    WEL_NTC = [];
    wardat = 'lettura function: there are not Wire Extensometer Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-Wire-RawData.csv']; % Specifico per Wire Extensometer Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-NTCWire-RawData.csv'];
    NAN = 0;
    DIS = 0;
    ChWEL = cell2mat(NodoWireExtensometerLink(:,4));
    WEL_NTC = cell2mat(NodoWireExtensometerLink(:,5));
    while NAN == 0
        if datenum(datainiWEL) == datenum(Data(1,1))
            tempoiniWEL = char(Data(1,2));
        end
        NodeType = 'Wire Extensometer Link';
        NodeNumWEL = num2str(cell2mat(NodoWireExtensometerLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiWEL ''' and Time >= ''' tempoiniWEL ''' and UnitName = ''' IDcentralina ...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumWEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniWEL = curs.Data;
        Ntempo = DATiniWEL;
        [r,c] = size(DATiniWEL);
        saltoWEL = 0;
        if r==1 && c==1
            textdatWEL = 'Warning: error reading Wire Extensometer Link (control unit data)!';
            fprintf(fileID,fmt,textdatWEL);
            saltoWEL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiWEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumWEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2WEL = curs.Data;
        Ndate = DAT2WEL;
        if saltoWEL == 1
            DATiniWEL = DAT2WEL;
        end
        Check = size(DAT2WEL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoWEL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATiniWEL = [DATiniWEL; DAT2WEL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniWEL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniWEL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniWEL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniWEL = T;
        else
            DATiniWEL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        if saltoWEL == 0
            % creo la matrice per i dati Wire Extensometer Link
            if Check(1,2) > 1
                DatiWireExtensometerLink = cell(rd+rt,rWEL);
                DatiNTCWireExtensometerLink = cell(rd+rt,rWEL);
            else
                DatiWireExtensometerLink = cell(rt,rWEL);
                DatiNTCWireExtensometerLink = cell(rt,rWEL);
            end
        else
            DatiWireExtensometerLink = cell(rd,rWEL);
            DatiNTCWireExtensometerLink = cell(rd,rWEL);
        end
        NWExtensometerLink = cell2mat(NodoWireExtensometerLink(:,2));
        col = 1;
        ii = 1;
        w = 0;
        while ii <= rWEL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NWExtensometerLink(ii,1);
            nNodo = num2str(nN);
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datainiWEL ''' and Time >= ''' tempoiniWEL ''' and  ToolNameID = ''' ...
                DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoWEL = curs.Data;
            % Step 2: Considero le date successive e scarico tutti i dati disponibili.
            comando = ['select Val0 from RawDataView where Date > ''' ...
                datainiWEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoWELd = curs.Data;
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            if ChWEL(ii,1) == 1
                % T
                DATnodoWEL_NTC = cell(1,1);
                DATnodoWELd_NTC = cell(1,1);
            elseif ChWEL(ii,1) >= 2
                if WEL_NTC(ii,1)==1
                    if ChWEL(ii,1) == 2
                        comando = ['select Val1 from RawDataView where Date = ''' ...
                            datainiWEL ''' and Time >= ''' tempoiniWEL ''' and  ToolNameID = ''' ...
                            DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                            NodeType ''' and NodeNum ='  nNodo ''];
                    elseif ChWEL(ii,1) == 4
                        comando = ['select Val2 from RawDataView where Date = ''' ...
                            datainiWEL ''' and Time >= ''' tempoiniWEL ''' and  ToolNameID = ''' ...
                            DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                            NodeType ''' and NodeNum ='  nNodo ''];
                    end
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoWEL_NTC = curs.Data;
                    % Dato
                    if ChWEL(ii,1) == 2
                        comando = ['select Val1 from RawDataView where Date > ''' ...
                            datainiWEL ''' and UnitName = ''' IDcentralina ...
                            ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                            NodeType ''' and NodeNum = '  nNodo ''];
                    elseif ChWEL(ii,1) == 4
                        comando = ['select Val2 from RawDataView where Date > ''' ...
                            datainiWEL ''' and UnitName = ''' IDcentralina ...
                            ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                            NodeType ''' and NodeNum = '  nNodo ''];
                    end
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoWELd_NTC = curs.Data;
                else
                    DATnodoWEL_NTC = cell(1,1);
                    DATnodoWELd_NTC = cell(1,1);
                end
            end
            
            [rn,~] = size(DATnodoWELd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoWELd(i,:) = DATnodoWELd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Wire Extensometer Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            if WEL_NTC(ii,1)==1
                [rnT,~] = size(DATnodoWELd_NTC); % controllo date
                if rnT < rd % normalmente sono uguali
                    for i = rnT+1:rd
                        DATnodoWELd_NTC(i,:) = DATnodoWELd_NTC(rn,:); % se mancano dati, copio gli ultimi
                    end
                    wardat = ['NTC of Node Wire Extensometer Link ' num2str(ii) ' does NOT work!'];
                    fprintf(fileID,fmt,wardat);
                    w = 1;
                end
            end
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoWEL == 0
                if ChWEL(ii,1) >= 2
                    if WEL_NTC(ii,1) == 1
                        DatiWireExtensometerLink(1:rt,col) = DATnodoWEL(:,:);
                        DatiNTCWireExtensometerLink(1:rt,col) = DATnodoWEL_NTC(:,:);
                        if Check(1,2) > 1
                            DatiWireExtensometerLink(rt+1:rt+rd,col) = DATnodoWELd(:,:);
                            DatiNTCWireExtensometerLink(rt+1:rt+rd,col) = DATnodoWELd_NTC(:,:);
                        end
                    else
                        DatiWireExtensometerLink(1:rt,col) = DATnodoWEL(:,:);
                        if Check(1,2) > 1
                            DatiWireExtensometerLink(rt+1:rt+rd,col) = DATnodoWELd(:,:);
                        end
                    end
                elseif ChWEL(ii,1) == 1
                    DatiWireExtensometerLink(1:rt,col) = DATnodoWEL(:,:);
                    if Check(1,2) > 1
                        DatiWireExtensometerLink(rt+1:rt+rd,col) = DATnodoWELd(:,:);
                    end
                end
            else
                if ChWEL(ii,1) >= 2
                    if WEL_NTC(ii,1) == 1
                       DatiWireExtensometerLink(1:rd,col) = DATnodoWEL(:,:);
                       DatiNTCWireExtensometerLink(1:rd,col) = DATnodoWEL_NTC(:,:);
                    else
                        DatiWireExtensometerLink(1:rd,col) = DATnodoWEL(:,:);
                    end
                elseif ChWEL(ii,1) == 1
                    DatiWireExtensometerLink(1:rd,col) = DATnodoWEL(:,:);
                end                
            end
            
            okdatWEL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rWEL) ' Wire Extensometer Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatWEL);
            ii = ii+1;
            col = col+1;
        end
        if w == 0
            wardat = 'There are not warning for Wire Extensometer Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        
        % Dati
        [r,c] = size(DatiWireExtensometerLink);
        ErrWireExtensometerLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroWEL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiWEL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
            % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',DatiWireExtensometerLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',DatiWireExtensometerLink(dis,jj)) == 0
                       DatiWireExtensometerLink = DatiWireExtensometerLink(dis:end,:);
                       DatiNTCWireExtensometerLink = DatiNTCWireExtensometerLink(dis:end,:);
                       DATiniWEL = DATiniWEL(dis:end,:);
                       [r,c] = size(DatiWireExtensometerLink);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(datainiWEL)+1 < now
                            datainiWEL = datestr(datenum(datainiWEL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesWEL = 0;
                        end
                    end
                end
            end
        end
        if DIS ~= 1 && yesWEL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiWireExtensometerLink(1,jj)));
                idxT = find(strcmp('---',DatiWireExtensometerLink(1,jj)));
                idxT2 = find(strcmp('-',DatiWireExtensometerLink(1,jj)));
                idxE2 = find(strcmp('Err2',DatiWireExtensometerLink(1,jj)));
                idxE255 = find(strcmp('Err255',DatiWireExtensometerLink(1,jj)));
                idxD = find(strcmp('DMUXe',DatiWireExtensometerLink(1,jj)));
                idxE1F = find(strcmp('Err1File',DatiWireExtensometerLink(1,jj)));
                idxN = find(strcmp('null',DatiWireExtensometerLink(1,jj)));
                idxRX = find(strcmp('No RX',DatiWireExtensometerLink(1,jj)));
                idxI = find(strcmp('id Error',DatiWireExtensometerLink(1,jj)));
                idxM = find(strcmp('MUX id Error',DatiWireExtensometerLink(1,jj)));
                idxC = find(strcmp('CH n. Error',DatiWireExtensometerLink(1,jj)));
                idxU = find(strcmp('Unknown Error',DatiWireExtensometerLink(1,jj)));
                idxMC = find(strcmp('MsgCorrup',DatiWireExtensometerLink(1,jj)));
                idxNS = find(strcmp('NotSupp',DatiWireExtensometerLink(1,jj)));
                idxU2 = find(strcmp('Unknown',DatiWireExtensometerLink(1,jj)));
                idxNA = find(strcmp('NotAv',DatiWireExtensometerLink(1,jj)));
                idxND = find(strcmp('No Data',DatiWireExtensometerLink(1,jj)));
                idxCE = find(strcmp('CH n. Er',DatiWireExtensometerLink(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxND);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of Wire Extensometer Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZeroWEL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(datainiWEL)+1 < now
                            datainiWEL = datestr(datenum(datainiWEL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesWEL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiWEL = datestr(datenum(datainiWEL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                DatiWireExtensometerLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrWireExtensometerLink(1,jj) = 1;
                                wardat = 'Data of Wire Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                dataini3DEL = datestr(datenum(dataini3DEL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiWireExtensometerLink(:,jj)));
                    idxT = find(strcmp('---',DatiWireExtensometerLink(:,jj)));
                    idxT2 = find(strcmp('-',DatiWireExtensometerLink(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiWireExtensometerLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiWireExtensometerLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiWireExtensometerLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiWireExtensometerLink(:,jj)));
                    idxN = find(strcmp('null',DatiWireExtensometerLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiWireExtensometerLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiWireExtensometerLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiWireExtensometerLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiWireExtensometerLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiWireExtensometerLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiWireExtensometerLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiWireExtensometerLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiWireExtensometerLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiWireExtensometerLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiWireExtensometerLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiWireExtensometerLink(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            wardat = 'Warning on the correction of Wire Extensometer Link: there are not data at the beginning of the array!';
                            fprintf(fileID,fmt,wardat);
                            NAN = 0;
                            ESCI = 1;
                            datainiWEL = datestr(datenum(datainiWEL)-1,'yyyy-mm-dd');
                        else
                            DatiWireExtensometerLink(indice,jj) = DatiWireExtensometerLink(indice-1,jj);
                            ErrWireExtensometerLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiWireExtensometerLink(nnn,jj)));
                        if check == 1
                            DatiWireExtensometerLink(nnn,jj) = DatiWireExtensometerLink(nnn-1,jj);
                            ErrWireExtensometerLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of Wire Extensometer Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiWireExtensometerLink = [DATiniWEL str2double(DatiWireExtensometerLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiWireExtensometerLink(a,1) == DatiWireExtensometerLink(a-1,1)
                        DatiWireExtensometerLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
        
        if ESCI == 0
            if sum(WEL_NTC(:,1))>0
                % NTC
                if DIS ~= 1 && yesWEL == 1
                   [r,c] = size(DatiNTCWireExtensometerLink);
                   Ncorr = 0;
                   if NuovoZeroWEL == 1
                       if isfile(RawDataFile) == 1
                           DatiRawNTC = csvread(RawDataFileNTC);
                           [rDR,cDR] = size(DatiRawNTC);
                           DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                           RawDateNTC = find(DatiRawNTC(:,1)<=datenum(datainiWEL));
                       else
                           rDR = 1;
                           cDR = 1;
                           RawDateNTC = [];
                           DatiRawNTC = [];
                       end
                   else
                       rDR = 1;
                       cDR = 1;
                       RawDateNTC = [];
                       DatiRawNTC = [];
                   end
                   for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTCWireExtensometerLink(1,jj)));
                        idxND = find(strcmp('No Data',DatiNTCWireExtensometerLink(1,jj)));
                        idxT3 = find(strcmp('---',DatiNTCWireExtensometerLink(1,jj)));
                        idxT = find(strcmp('-',DatiNTCWireExtensometerLink(1,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTCWireExtensometerLink(1,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTCWireExtensometerLink(1,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTCWireExtensometerLink(1,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTCWireExtensometerLink(1,jj)));
                        idxN = find(strcmp('null',DatiNTCWireExtensometerLink(1,jj)));
                        idxRX = find(strcmp('No RX',DatiNTCWireExtensometerLink(1,jj)));
                        idxI = find(strcmp('id Error',DatiNTCWireExtensometerLink(1,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTCWireExtensometerLink(1,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTCWireExtensometerLink(1,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTCWireExtensometerLink(1,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTCWireExtensometerLink(1,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTCWireExtensometerLink(1,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTCWireExtensometerLink(1,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTCWireExtensometerLink(1,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTCWireExtensometerLink(1,jj)));
                        idx = union(idxE,idxT3);
                        idx = union(idx,idxND);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxT);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        [ri,~] = size(idx);
                        if ri == 1
                            wardat = 'Warning on the correction of NTC of Wire Extensometer Link nodes: there are not data at the beginning of the array!';
                            fprintf(fileID,fmt,wardat);
                            if rDR==1 && cDR==1 || NuovoZeroWEL == 0 % il file non è mai stato scritto, cerco la data precedente
                                NAN = 0;
                                ESCI = 1;
                                if datenum(datainiWEL)+1 < now
                                    datainiWEL = datestr(datenum(datainiWEL)+1,'yyyy-mm-dd');
                                    break
                                else
                                    yesWEL = 0;
                                end
                            else
                                if isempty(RawDateNTC) == 1
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiWEL = datestr(datenum(datainiWEL)-1,'yyyy-mm-dd');
                                    break
                                else
                                    DatiNTCWireExtensometerLink(1,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                    if ErrWireExtensometerLink(1,jj) == 0
                                        ErrWireExtensometerLink(1,jj) = 0.5;
                                    else
                                        ErrWireExtensometerLink(1,jj) = 1;
                                    end
                                    wardat = 'Data of NTC of Wire Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                end
                            end
                        end
                   end
                   for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTCWireExtensometerLink(:,jj)));
                        idxND = find(strcmp('No Data',DatiNTCWireExtensometerLink(:,jj)));
                        idxT3 = find(strcmp('---',DatiNTCWireExtensometerLink(:,jj)));
                        idxT = find(strcmp('-',DatiNTCWireExtensometerLink(:,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTCWireExtensometerLink(:,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTCWireExtensometerLink(:,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTCWireExtensometerLink(:,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTCWireExtensometerLink(:,jj)));
                        idxN = find(strcmp('null',DatiNTCWireExtensometerLink(:,jj)));
                        idxRX = find(strcmp('No RX',DatiNTCWireExtensometerLink(:,jj)));
                        idxI = find(strcmp('id Error',DatiNTCWireExtensometerLink(:,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTCWireExtensometerLink(:,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTCWireExtensometerLink(:,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTCWireExtensometerLink(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTCWireExtensometerLink(:,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTCWireExtensometerLink(:,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTCWireExtensometerLink(:,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTCWireExtensometerLink(:,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTCWireExtensometerLink(:,jj)));
                        idx = union(idxE,idxT3);
                        idx = union(idx,idxND);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxT);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                if DIS == 1
                                    wardat = 'Warning on the correction of NTC of Wire Extensometer Link: there are not data at the beginning of the array!';
                                    fprintf(fileID,fmt,wardat);
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiWEL = datestr(datenum(datainiWEL)+1,'yyyy-mm-dd');
                                    break
                                else
                                    wardat = 'Warning on the correction of NTC of Wire Extensometer Link nodes: there are not data at the beginning of the array!';
                                    fprintf(fileID,fmt,wardat);
                                    if rDR==1 && cDR==1 % il file non è mai stato scritto
                                        NAN = 0;
                                        ESCI = 1;
                                        datainiWEL = datestr(datenum(datainiWEL)-1,'yyyy-mm-dd');
                                        break
                                    else
                                        if isempty(RawDateNTC) == 1
                                            NAN = 0;
                                            ESCI = 1;
                                            datainiWEL = datestr(datenum(datainiWEL)-1,'yyyy-mm-dd');
                                            break
                                        else
                                            DatiNTCWireExtensometerLink(indice,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                            if ErrWireExtensometerLink(indice,jj) == 0
                                                ErrWireExtensometerLink(indice,jj) = 0.5;
                                            else
                                                ErrWireExtensometerLink(indice,jj) = 1;
                                            end
                                            wardat = 'Data of Wire Extensometer Link nodes corrected using Raw Data of Excel file.';
                                            fprintf(fileID,fmt,wardat);
                                            Ncorr = Ncorr+ri;
                                            NAN = 1;
                                        end
                                    end
                                end
                            else
                                DatiNTCWireExtensometerLink(indice,jj) = DatiNTCWireExtensometerLink(indice-1,jj);
                                if ErrWireExtensometerLink(indice,jj) == 0
                                    ErrWireExtensometerLink(indice,jj) = 0.5;
                                else
                                    ErrWireExtensometerLink(indice,jj) = 1;
                                end
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(DatiNTCWireExtensometerLink(nnn,jj)));
                            if check == 1
                                DatiNTCWireExtensometerLink(nnn,jj) = DatiNTCWireExtensometerLink(nnn-1,jj);
                                if ErrWireExtensometerLink(indice,jj) == 0
                                    ErrWireExtensometerLink(indice,jj) = 0.5;
                                else
                                    ErrWireExtensometerLink(indice,jj) = 1;
                                end
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of NTC Wire Extensometer Link data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    DatiNTCWireExtensometerLink = [DATiniWEL str2double(DatiNTCWireExtensometerLink)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if DatiNTCWireExtensometerLink(a,1) == DatiNTCWireExtensometerLink(a-1,1)
                            DatiNTCWireExtensometerLink(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            end
        end                 
    end
    if NuovoZeroWEL == 1 && yesWEL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiWireExtensometerLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiWireExtensometerLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiWireExtensometerLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiWireExtensometerLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiWireExtensometerLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
        
        if sum(WEL_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTCWireExtensometerLink(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTCWireExtensometerLink(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTCWireExtensometerLink(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTCWireExtensometerLink(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTCWireExtensometerLink;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = DatiWireExtensometerLink;
        if sum(WEL_NTC(:,1))>0
            DatiNTC = DatiNTCWireExtensometerLink;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(WEL_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
end

%%  MultiPoint Borehole Extensometer Link
if  yesMPBEL == 0 % Non ci sono Multipoint borehole rod extensometer
    DatiMultiPointExtensometer = [];
    DatiNTCMultiPointExtensometer = [];
    ErrMultiPointExtensometer = [];
    ErrNTCMultiPointExtensometer = [];
    ChMPBEL = [];
    MPBEL_NTC = [];
    wardat = 'lettura function: there are not Multi Point Extensometer Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-MPBEL-RawData.csv']; % Specifico per MPBE Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-NTCMPBEL-RawData.csv']; % Specifico per NTC MPBE Link
    ChMPBEL = cell2mat(NodoMultiPointRodExtensometer(:,4));
    MPBEL_NTC = cell2mat(NodoMultiPointRodExtensometer(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(datainiMPBEL) == datenum(Data(1,1))
            tempoiniMPBEL = char(Data(1,2));
        end
        NodeType = 'Multipoint borehole rod extensometer';
        NodeNumMPBEL = num2str(cell2mat(NodoMultiPointRodExtensometer(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and UnitName = ''' IDcentralina ...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumMPBEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniMPBEL = curs.Data;
        Ntempo = DATiniMPBEL;
        [r,c] = size(DATiniMPBEL);
        saltoMPBEL = 0;
        if r==1 && c==1
            textdatMPBEL = 'Warning: error reading Multipoint borehole rod extensometer (control unit data)!';
            fprintf(fileID,fmt,textdatMPBEL);
            saltoMPBEL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumMPBEL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2MPBEL = curs.Data;
        Ndate = DAT2MPBEL;
        if saltoMPBEL == 1
            DATiniMPBEL = DAT2MPBEL;
        end
        Check = size(DAT2MPBEL);
        if Check(1,2) > 1 % ho superato il primo giorno
            % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
            if saltoMPBEL == 0
                DATiniMPBEL = [DATiniMPBEL; DAT2MPBEL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniMPBEL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniMPBEL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniMPBEL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniMPBEL = T;
        else
            DATiniMPBEL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Multipoint borehole rod extensometer
        if saltoMPBEL == 0
            if Check(1,2) > 1
                DatiMultiPointExtensometer = cell(rd+rt,rMPBEL*NumBasi);
                DatiNTCMultiPointExtensometer = cell(rd+rt,rMPBEL*NumBasi);
            else
                DatiMultiPointExtensometer = cell(rt,rMPBEL*NumBasi);
                DatiNTCMultiPointExtensometer = cell(rt,rMPBEL*NumBasi);
            end
        else
            DatiMultiPointExtensometer = cell(rd,rMPBEL*NumBasi);
            DatiNTCMultiPointExtensometer = cell(rd,rMPBEL*NumBasi);
        end
        NMPBExtensometerLink = cell2mat(NodoMultiPointRodExtensometer(:,2));
        col = 1;
        ii = 1;
        w = 0;
        while ii <= rMPBEL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NMPBExtensometerLink(ii,1);
            nNodo = num2str(nN);
            % 1 Base
            if NumBasi == 1
                % Dato
                comando = ['select Val0 from RawDataView where Date = ''' ...
                    datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                    DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoMPBEL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0 from RawDataView where Date > ''' ...
                    datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoMPBELd = curs.Data;
                if ChMPBEL(ii,1) >= 2 && MPBEL_NTC(ii,1) == 1 % leggo la temperatura
                    % T
                    comando = ['select Val1 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL_NTC = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val1 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd_NTC = curs.Data;
                else % non la leggo
                    DATnodoMPBEL_NTC = 0;
                    DATnodoMPBELd_NTC = 0;
                end
            % 2 Basi    
            elseif NumBasi == 2
                if ChMPBEL(ii,1) == 4
                    % Dato
                    comando = ['select Val0, Val2 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val0, Val2 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd = curs.Data;
                else
                    % Dato
                    comando = ['select Val0, Val1 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val0, Val1 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd = curs.Data;
                end
                if ChMPBEL(ii,1) >= 3 && MPBEL_NTC(ii,1) == 1 % leggo la temperatura
                    % T
                    comando = ['select Val2 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL_NTC = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val2 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd_NTC = curs.Data;
                else % non la leggo
                    DATnodoMPBEL_NTC = 0;
                    DATnodoMPBELd_NTC = 0;
                end
            elseif NumBasi == 3
                if ChMPBEL(ii,1) == 6
                    % Dato
                    comando = ['select Val0, Val2, Val4 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val0, Val2, Val4 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd = curs.Data;
                else
                    % Dato
                    comando = ['select Val0, Val1, Val2 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val0, Val1, Val2 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd = curs.Data;
                end
                if ChMPBEL(ii,1) >= 4 && MPBEL_NTC(ii,1) == 1 % leggo la temperatura della prima base
                    % T
                    comando = ['select Val3 from RawDataView where Date = ''' ...
                        datainiMPBEL ''' and Time >= ''' tempoiniMPBEL ''' and  ToolNameID = '''...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBEL_NTC = curs.Data;
                    % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                    comando = ['select Val3 from RawDataView where Date > ''' ...
                        datainiMPBEL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoMPBELd_NTC = curs.Data;
                else
                    DATnodoMPBEL_NTC = 0;
                    DATnodoMPBELd_NTC = 0;       
                end
            end
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
           
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if NumBasi == 1
                if saltoMPBEL == 0
                    DatiMultiPointExtensometer(1:rt,col) = DATnodoMPBEL(:,:);
                    if ChMPBEL(ii) >= 2 && MPBEL_NTC(ii) == 1
                        DatiNTCMultiPointExtensometer(1:rt,col) = DATnodoMPBEL_NTC(:,:);
                    end
                else
                    DatiMultiPointExtensometer(1:rd,col) = DATnodoMPBEL(:,:);
                    if ChMPBEL(ii) >= 2 && MPBEL_NTC(ii) == 1
                        DatiNTCMultiPointExtensometer(1:rd,col) = DATnodoMPBEL_NTC(:,:);
                    end
                end
    
            elseif NumBasi == 2
                if saltoMPBEL == 0
                    DatiMultiPointExtensometer(1:rt,col:col+1) = DATnodoMPBEL(:,:);
                    if ChMPBEL(ii) >= 3 && MPBEL_NTC(ii) == 1 || ChMPBEL(ii) == 4 && MPBEL_NTC(ii) == 1
                        DatiNTCMultiPointExtensometer(1:rt,col) = DATnodoMPBEL_NTC(:,:);
                    end
                else
                    DatiMultiPointExtensometer(1:rd,col:col+1) = DATnodoMPBEL(:,:);
                    if ChMPBEL(ii) >= 3 && MPBEL_NTC(ii) == 1 || ChMPBEL(ii) == 4 && MPBEL_NTC(ii) == 1
                        DatiNTCMultiPointExtensometer(1:rd,col) = DATnodoMPBEL_NTC(:,:);
                    end
                end
                
            elseif NumBasi == 3
                if saltoMPBEL == 0
                    DatiMultiPointExtensometer(1:rt,col:col+2) = DATnodoMPBEL(:,:);
                    if ChMPBEL(ii) >= 4 && MPBEL_NTC(ii) == 1
                        DatiNTCMultiPointExtensometer(1:rt,col) = DATnodoMPBEL_NTC(:,:);
                    end
                else
                    DatiMultiPointExtensometer(1:rd,col:col+2) = DATnodoMPBEL(:,:);
                    if ChMPBEL(ii) >= 4 && MPBEL_NTC(ii) == 1
                        DatiNTCMultiPointExtensometer(1:rd,col) = DATnodoMPBEL_NTC(:,:);
                    end
                end
            end
            [rn,~] = size(DATnodoMPBELd); % controllo date
            [rnT,~] = size(DATnodoMPBELd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoMPBELd(i,:) = DATnodoMPBELd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Multipoint Borehole Rod Extensometer ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            if rnT < rd % normalmente sono uguali
                for i = rnT+1:rd
                    DATnodoMPBELd_NTC(i,:) = DATnodoMPBELd_NTC(rnT,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node Multipoint Borehole Rod Extensometer ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoMPBEL == 0
                    if NumBasi == 1
                        DatiMultiPointExtensometer(rt+1:rt+rd,col) = DATnodoMPBELd(:,:);
                        if ChMPBEL(ii) >= 2 && MPBEL_NTC(ii) == 1
                            DatiNTCMultiPointExtensometer(rt+1:rt+rd,col) = DATnodoMPBELd_NTC(:,:);
                        end
                    elseif NumBasi == 2
                        DatiMultiPointExtensometer(rt+1:rt+rd,col:col+1) = DATnodoMPBELd(:,:);
                        if ChMPBEL(ii) >= 3 && MPBEL_NTC(ii) == 1
                            DatiNTCMultiPointExtensometer(rt+1:rt+rd,col) = DATnodoMPBELd_NTC(:,:);
                        end
                    elseif NumBasi == 3
                        DatiMultiPointExtensometer(rt+1:rt+rd,col:col+2) = DATnodoMPBELd(:,:);
                        if ChMPBEL(ii) >= 4 && MPBEL_NTC(ii) == 1
                            DatiNTCMultiPointExtensometer(rt+1:rt+rd,col) = DATnodoMPBELd_NTC(:,:);
                        end
                    end
                end
            else
                DatiMultiPointExtensometer = DatiMultiPointExtensometer(1:end,:);
                DatiNTCMultiPointExtensometer = DatiNTCMultiPointExtensometer(1:end,:);
            end
            okdatMPBEL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rMPBEL) ' Multipoint Borehole Rod Extensometer nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatMPBEL);
            ii = ii+1;
            if NumBasi == 1
                col = col+1;
            elseif NumBasi == 2
                col = col+2;
            elseif NumBasi == 3
                col = col+3;
            elseif NumBasi == 4
                col = col+4;
            elseif NumBasi == 5
                col = col+5;
            elseif NumBasi == 6
                col = col+6;
            end
        end
        if w == 0
            wardat = 'There are not warning for Multipoint Borehole Rod Extensometer!';
            fprintf(fileID,fmt,wardat);
        end

        % ---- Dati Multibase --------------
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiMultiPointExtensometer);
        ErrMultiPointExtensometer = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroMPBEL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiMPBEL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
            % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',DatiMultiPointExtensometer(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',DatiMultiPointExtensometer(dis,jj)) == 0
                       DatiMultiPointExtensometer = DatiMultiPointExtensometer(dis:end,:);
                       DatiNTCMultiPointExtensometer = DatiNTCMultiPointExtensometer(dis:end,:);
                       DATiniMPBEL = DATiniMPBEL(dis:end,:);
                       [r,c] = size(DatiMultiPointExtensometer);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(datainiMPBEL)+1 < now
                            datainiMPBEL = datestr(datenum(datainiMPBEL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesMPBEL = 0;
                        end
                    end
                end
            end
        end
        if DIS ~= 1 && yesMPBEL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiMultiPointExtensometer(1,jj)));
                idxT = find(strcmp('---',DatiMultiPointExtensometer(1,jj)));
                idxT2 = find(strcmp('-',DatiMultiPointExtensometer(1,jj)));
                idxE2 = find(strcmp('Err2',DatiMultiPointExtensometer(1,jj)));
                idxE255 = find(strcmp('Err255',DatiMultiPointExtensometer(1,jj)));
                idxD = find(strcmp('DMUXe',DatiMultiPointExtensometer(1,jj)));
                idxE1F = find(strcmp('Err1File',DatiMultiPointExtensometer(1,jj)));
                idxN = find(strcmp('null',DatiMultiPointExtensometer(1,jj)));
                idxRX = find(strcmp('No RX',DatiMultiPointExtensometer(1,jj)));
                idxI = find(strcmp('id Error',DatiMultiPointExtensometer(1,jj)));
                idxM = find(strcmp('MUX id Error',DatiMultiPointExtensometer(1,jj)));
                idxC = find(strcmp('CH n. Error',DatiMultiPointExtensometer(1,jj)));
                idxU = find(strcmp('Unknown Error',DatiMultiPointExtensometer(1,jj)));
                idxMC = find(strcmp('MsgCorrup',DatiMultiPointExtensometer(1,jj)));
                idxNS = find(strcmp('NotSupp',DatiMultiPointExtensometer(1,jj)));
                idxU2 = find(strcmp('Unknown',DatiMultiPointExtensometer(1,jj)));
                idxNA = find(strcmp('NotAv',DatiMultiPointExtensometer(1,jj)));
                idxND = find(strcmp('No Data',DatiMultiPointExtensometer(1,jj)));
                idxCE = find(strcmp('CH n. Er',DatiMultiPointExtensometer(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxND);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of MultiPoint Rod Extensometer Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZeroMPBEL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(datainiMPBEL)+1 < now
                            datainiMPBEL = datestr(datenum(datainiMPBEL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesMPBEL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiMPBEL = datestr(datenum(datainiMPBEL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                DatiMultiPointExtensometer(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrMultiPointExtensometer(1,jj) = 1;
                                wardat = 'Data of MultiPoint Rod Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                datainiMPBEL = datestr(datenum(datainiMPBEL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiMultiPointExtensometer(:,jj)));
                    idxT = find(strcmp('---',DatiMultiPointExtensometer(:,jj)));
                    idxT2 = find(strcmp('-',DatiMultiPointExtensometer(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiMultiPointExtensometer(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiMultiPointExtensometer(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiMultiPointExtensometer(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiMultiPointExtensometer(:,jj)));
                    idxN = find(strcmp('null',DatiMultiPointExtensometer(:,jj)));
                    idxRX = find(strcmp('No RX',DatiMultiPointExtensometer(:,jj)));
                    idxI = find(strcmp('id Error',DatiMultiPointExtensometer(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiMultiPointExtensometer(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiMultiPointExtensometer(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiMultiPointExtensometer(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiMultiPointExtensometer(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiMultiPointExtensometer(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiMultiPointExtensometer(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiMultiPointExtensometer(:,jj)));
                    idxND = find(strcmp('No Data',DatiMultiPointExtensometer(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiMultiPointExtensometer(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiMPBEL = datestr(datenum(datainiMPBEL)-1,'yyyy-mm-dd');
                        else
                            DatiMultiPointExtensometer(indice,jj) = DatiMultiPointExtensometer(indice-1,jj);
                            ErrMultiPointExtensometer(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiMultiPointExtensometer(nnn,jj)));
                        if check == 1
                            DatiMultiPointExtensometer(nnn,jj) = DatiMultiPointExtensometer(nnn-1,jj);
                            ErrMultiPointExtensometer(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of Multipoint Borehole Rod Extensometer data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiMultiPointExtensometer = [DATiniMPBEL str2double(DatiMultiPointExtensometer)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiMultiPointExtensometer(a,1) == DatiMultiPointExtensometer(a-1,1)
                        DatiMultiPointExtensometer(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
        
        if ESCI == 0
            if sum(MPBEL_NTC(:,1))>0
                if DIS ~= 1 && yesMPBEL == 1
                    % ---- Dati NTC Multibase --------------
                    % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
                    %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
                    [r,c] = size(DatiNTCMultiPointExtensometer);
                    ErrNTCMultiPointExtensometer = zeros(r,c);
                    Ncorr = 0;
                    if NuovoZeroMPBEL == 1
                        if isfile(RawDataFile) == 1
                            DatiRawNTC = csvread(RawDataFileNTC);
                            [rDR,cDR] = size(DatiRawNTC);
                            DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                            RawDateNTC = find(DatiRawNTC(:,1)<=datenum(datainiMPBEL));
                        else
                            rDR = 1;
                            cDR = 1;
                            RawDateNTC = [];
                            DatiRawNTC = [];
                        end
                    else
                        rDR = 1;
                        cDR = 1;
                        RawDateNTC = [];
                        DatiRawNTC = [];
                    end
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTCMultiPointExtensometer(1,jj)));
                        idxT = find(strcmp('---',DatiNTCMultiPointExtensometer(1,jj)));
                        idxT2 = find(strcmp('-',DatiNTCMultiPointExtensometer(1,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTCMultiPointExtensometer(1,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTCMultiPointExtensometer(1,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTCMultiPointExtensometer(1,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTCMultiPointExtensometer(1,jj)));
                        idxN = find(strcmp('null',DatiNTCMultiPointExtensometer(1,jj)));
                        idxRX = find(strcmp('No RX',DatiNTCMultiPointExtensometer(1,jj)));
                        idxI = find(strcmp('id Error',DatiNTCMultiPointExtensometer(1,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTCMultiPointExtensometer(1,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTCMultiPointExtensometer(1,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTCMultiPointExtensometer(1,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTCMultiPointExtensometer(1,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTCMultiPointExtensometer(1,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTCMultiPointExtensometer(1,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTCMultiPointExtensometer(1,jj)));
                        idxND = find(strcmp('No Data',DatiNTCMultiPointExtensometer(1,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTCMultiPointExtensometer(1,jj)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxT2);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxND);
                        [ri,~] = size(idx);
                        if ri == 1
                            wardat = 'Warning on the correction of NTC of MultiPoint Rod Extensometer Link nodes: there are not data at the beginning of the array!';
                            fprintf(fileID,fmt,wardat);
                            if rDR==1 && cDR==1 || NuovoZeroMPBEL == 0 % il file non è mai stato scritto, cerco la data precedente
                                NAN = 0;
                                ESCI = 1;
                                if datenum(datainiMPBEL)+1 < now
                                    datainiMPBEL = datestr(datenum(datainiMPBEL)+1,'yyyy-mm-dd');
                                    break
                                else
                                    yesMPBEL = 0;
                                end
                            else
                                if isempty(RawDateNTC) == 1
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiMPBEL = datestr(datenum(datainiMPBEL)-1,'yyyy-mm-dd');
                                    break
                                else
                                    DatiNTCMultiPointExtensometer(1,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                    if ErrMultiPointExtensometer(1,jj) == 0
                                        ErrMultiPointExtensometer(1,jj) = 0.5;
                                    else
                                        ErrMultiPointExtensometer(1,jj) = 1;
                                    end
                                    wardat = 'Data of NTC of Multi Point Rod Extensometer Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                end
                            end
                        end
                    end
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTCMultiPointExtensometer(:,jj)));
                        idxT3 = find(strcmp('---',DatiNTCMultiPointExtensometer(:,jj)));
                        idxT = find(strcmp('-',DatiNTCMultiPointExtensometer(:,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTCMultiPointExtensometer(:,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTCMultiPointExtensometer(:,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTCMultiPointExtensometer(:,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTCMultiPointExtensometer(:,jj)));
                        idxN = find(strcmp('null',DatiNTCMultiPointExtensometer(:,jj)));
                        idxRX = find(strcmp('No RX',DatiNTCMultiPointExtensometer(:,jj)));
                        idxI = find(strcmp('id Error',DatiNTCMultiPointExtensometer(:,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTCMultiPointExtensometer(:,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTCMultiPointExtensometer(:,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTCMultiPointExtensometer(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTCMultiPointExtensometer(:,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTCMultiPointExtensometer(:,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTCMultiPointExtensometer(:,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTCMultiPointExtensometer(:,jj)));
                        idxND = find(strcmp('No Data',DatiNTCMultiPointExtensometer(:,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTCMultiPointExtensometer(:,jj)));
                        idx = union(idxE,idxT3);
                        idx = union(idx,idxND);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxT);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiMPBEL = datestr(datenum(datainiMPBEL)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiNTCMultiPointExtensometer(indice,jj) = DatiNTCMultiPointExtensometer(indice-1,jj);
                                ErrNTCMultiPointExtensometer(indice,jj) = 1;
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(DatiNTCMultiPointExtensometer(nnn,jj)));
                            if check == 1
                                DatiNTCMultiPointExtensometer(nnn,jj) = DatiNTCMultiPointExtensometer(nnn-1,jj);
                                if ErrMultiPointExtensometer(indice,jj) == 0
                                    ErrMultiPointExtensometer(indice,jj) = 0.5;
                                else
                                    ErrMultiPointExtensometer(indice,jj) = 1;
                                end
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of NTC of Multipoint Borehole Rod Extensometer data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    DatiNTCMultiPointExtensometer = [DATiniMPBEL str2double(DatiNTCMultiPointExtensometer)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if DatiNTCMultiPointExtensometer(a,1) == DatiNTCMultiPointExtensometer(a-1,1)
                            DatiNTCMultiPointExtensometer(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            else
                ErrNTCMultiPointExtensometer = 0;
            end
        end
    end
    if NuovoZeroMPBEL == 1 && yesMPBEL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiMultiPointExtensometer(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiMultiPointExtensometer(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiMultiPointExtensometer(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiMultiPointExtensometer(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiMultiPointExtensometer;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
        
        if sum(MPBEL_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRaw(:,1)<=DatiNTCMultiPointExtensometer(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTCMultiPointExtensometer(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTCMultiPointExtensometer(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTCMultiPointExtensometer(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTCMultiPointExtensometer;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        else
            ErrNTCMultiPointExtensometer = 0;
        end
    else
        Dati = DatiMultiPointExtensometer;
        if sum(MPBEL_NTC(:,1))>0
            DatiNTC = DatiNTCMultiPointExtensometer;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(MPBEL_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
end

%%  Analog Link
if  yesAL == 0 % Non ci sono Analog Link
    DatiAnalogLink = [];
    ErrAnalogLink = [];
    ChAL = [];
    AL_NTC = [];
    wardat = 'lettura function: there are not Analog Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-AL-RawData.csv']; % Specifico per An Link
    ChAL = cell2mat(NodoAnalogLink(:,4));
    AL_NTC = cell2mat(NodoAnalogLink(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(datainiAL) == datenum(Data(1,1))
            tempoiniAL = char(Data(1,2));
        end
        NodeType = 'Analog Link';
        NodeNumAL = num2str(cell2mat(NodoAnalogLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiAL ''' and Time >= ''' tempoiniAL ''' and UnitName = ''' IDcentralina ''' and  ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumAL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniAL = curs.Data;
        Ntempo = DATiniAL;
        [r,c] = size(DATiniAL);
        saltoAL = 0;
        if r==1 && c==1
            textdatAL = 'Warning: error reading Analog Link (control unit data)!';
            fprintf(fileID,fmt,textdatAL);
            saltoAL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiAL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' NodeNumAL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2AL = curs.Data;
        Ndate = DAT2AL;
        if saltoAL == 1
            DATiniAL = DAT2AL;
        end
        Check = size(DAT2AL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoAL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATiniAL = [DATiniAL; DAT2AL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniAL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniAL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniAL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniAL = T;
        else
            DATiniAL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Analog Link
        if saltoAL == 0
            if Check(1,2) > 1
                DatiAnalogLink = cell(rd+rt,rAL*ChAL);
            else
                DatiAnalogLink = cell(rt,rAL*ChAL);
            end
        else
            DatiAnalogLink = cell(rd,rAL*ChAL);
        end
        NAnalogLink = cell2mat(NodoAnalogLink(:,2));
        col = ChAL;
        ii = 1;
        w = 0;
        while ii <= rAL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NAnalogLink(ii,1);
            nNodo = num2str(nN);
            if ChAL == 1
                comando = ['select Val0 from RawDataView where Date = ''' ...
                    datainiAL ''' and Time >= ''' tempoiniAL ''' and  ToolNameID = '''...
                    DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = '''...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0 from RawDataView where Date > ''' ...
                    datainiAL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoALd = curs.Data;
            elseif AnalogCh == 2
                comando = ['select Val0, Val1 from RawDataView where Date = ''' ...
                    datainiAL ''' and Time >= ''' tempoiniAL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0, Val1 from RawDataView where Date > ''' ...
                    datainiAL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoALd = curs.Data;
            elseif AnalogCh == 3
                comando = ['select Val0, Val1, Val2 from RawDataView where Date = ''' ...
                    datainiAL ''' and Time >= ''' tempoiniAL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0, Val1, Val2 from RawDataView where Date > ''' ...
                    datainiAL ''' and ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina...
                    ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoALd = curs.Data;
            elseif AnalogCh == 4
                comando = ['select Val0, Val1, Val2, Val3 from RawDataView where Date = ''' ...
                    datainiAL ''' and Time >= ''' tempoiniAL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0, Val1, Val2, Val3 from RawDataView where Date > ''' ...
                    datainiAL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoALd = curs.Data;
            elseif AnalogCh == 5
                comando = ['select Val0, Val1, Val2, Val3, Val4 from RawDataView where Date = ''' ...
                    datainiAL ''' and Time >= ''' tempoiniAL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0, Val1, Val2, Val3, Val4 from RawDataView where Date > ''' ...
                    datainiAL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoALd = curs.Data;
            elseif AnalogCh == 6
                comando = ['select Val0, Val1, Val2, Val3, Val4, Val5 from RawDataView where Date = ''' ...
                    datainiAL ''' and Time >= ''' tempoiniAL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                % Step 2: Considero le date successive e scarico tutti i dati disponibili.
                comando = ['select Val0, Val1, Val2, Val3, Val4, Val5 from RawDataView where Date > ''' ...
                    datainiAL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoALd = curs.Data;
            end
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoAL == 0
                if AnalogCh == 1
                    DatiAnalogLink(1:rt,col) = DATnodoAL(:,:);
                elseif AnalogCh == 2
                    ini_col = col-1;
                    DatiAnalogLink(1:rt,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 3
                    ini_col = col-2;
                    DatiAnalogLink(1:rt,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 4
                    ini_col = col-3;
                    DatiAnalogLink(1:rt,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 5
                    ini_col = col-4;
                    DatiAnalogLink(1:rt,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 6
                    ini_col = col-5;
                    DatiAnalogLink(1:rt,ini_col:col) = DATnodoAL(:,:);
                end
            else
                if AnalogCh == 1
                    DatiAnalogLink(1:rd,col) = DATnodoAL(:,:);
                elseif AnalogCh == 2
                    ini_col = col-1;
                    DatiAnalogLink(1:rd,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 3
                    ini_col = col-2;
                    DatiAnalogLink(1:rd,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 4
                    ini_col = col-3;
                    DatiAnalogLink(1:rd,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 5
                    ini_col = col-4;
                    DatiAnalogLink(1:rd,ini_col:col) = DATnodoAL(:,:);
                elseif AnalogCh == 6
                    ini_col = col-5;
                    DatiAnalogLink(1:rd,ini_col:col) = DATnodoAL(:,:);
                end
            end
            [rn,~] = size(DATnodoALd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoALd(i,:) = DATnodoALd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Analog Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if Check(1,2) > 1
                if saltoAL == 0
                    if ChAL == 1
                        DatiAnalogLink(rt+1:rt+rd,col) = DATnodoALd(:,:);
                    else
                        DatiAnalogLink(rt+1:rt+rd,ini_col:col) = DATnodoALd(:,:);
                    end
                end
            else
                DatiAnalogLink = DatiAnalogLink(1:end,:);
            end
            okdatAL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rAL) ' Analog Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatAL);
            ii = ii+1;
            if ChAL == 1
                col = col+1;
            elseif ChAL == 2
                col = col+2;
            elseif ChAL == 3
                col = col+3;
            elseif ChAL == 4
                col = col+4;
            elseif ChAL == 5
                col = col+5;
            elseif ChAL == 6
                col = col+6;
            end
        end
        if w == 0
            wardat = 'There are not warning for Analog Link!';
            fprintf(fileID,fmt,wardat);
        end
        
        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(DatiAnalogLink);
        ErrAnalogLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroAL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiAL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
             % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',DatiAnalogLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',DatiAnalogLink(dis,jj)) == 0
                        DatiAnalogLink = DatiAnalogLink(dis:end,:);
                        DATiniAL = DATiniAL(dis:end,:);
                        [r,c] = size(DatiAnalogLink);
                        break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(datainiAL)+1 < now
                            datainiAL = datestr(datenum(datainiAL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesAL = 0;
                        end
                    end
                end
            end
        end
        if DIS ~= 1 && yesAL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiAnalogLink(1,jj)));
                idxT = find(strcmp('---',DatiAnalogLink(1,jj)));
                idxT2 = find(strcmp('-',DatiAnalogLink(1,jj)));
                idxE2 = find(strcmp('Err2',DatiAnalogLink(1,jj)));
                idxE255 = find(strcmp('Err255',DatiAnalogLink(1,jj)));
                idxD = find(strcmp('DMUXe',DatiAnalogLink(1,jj)));
                idxE1F = find(strcmp('Err1File',DatiAnalogLink(1,jj)));
                idxN = find(strcmp('null',DatiAnalogLink(1,jj)));
                idxRX = find(strcmp('No RX',DatiAnalogLink(1,jj)));
                idxI = find(strcmp('id Error',DatiAnalogLink(1,jj)));
                idxM = find(strcmp('MUX id Error',DatiAnalogLink(1,jj)));
                idxC = find(strcmp('CH n. Error',DatiAnalogLink(1,jj)));
                idxU = find(strcmp('Unknown Error',DatiAnalogLink(1,jj)));
                idxDis = find(strcmp('Dis.',DatiAnalogLink(1,jj)));
                idxMC = find(strcmp('MsgCorrup',DatiAnalogLink(1,jj)));
                idxNS = find(strcmp('NotSupp',DatiAnalogLink(1,jj)));
                idxU2 = find(strcmp('Unknown',DatiAnalogLink(1,jj)));
                idxNA = find(strcmp('NotAv',DatiAnalogLink(1,jj)));
                idxND = find(strcmp('No Data',DatiAnalogLink(1,jj)));
                idxCE = find(strcmp('CH n. Er',DatiAnalogLink(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxND);
                idx = union(idx,idxCE);
                idx = union(idx,idxT2);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxDis);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of Analog Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZeroAL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(datainiAL)+1 < now
                            datainiAL = datestr(datenum(datainiAL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesAL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiAL = datestr(datenum(datainiAL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                DatiAnalogLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrAnalogLink(1,jj) = 1;
                                wardat = 'Data of Analog Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                datainiAL = datestr(datenum(datainiAL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiAnalogLink(:,jj)));
                    idxT = find(strcmp('---',DatiAnalogLink(:,jj)));
                    idxT2 = find(strcmp('-',DatiAnalogLink(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiAnalogLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiAnalogLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiAnalogLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiAnalogLink(:,jj)));
                    idxN = find(strcmp('null',DatiAnalogLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiAnalogLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiAnalogLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiAnalogLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiAnalogLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiAnalogLink(:,jj)));
                    idxDis = find(strcmp('Dis.',DatiAnalogLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiAnalogLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiAnalogLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiAnalogLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiAnalogLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiAnalogLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiAnalogLink(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxND);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxDis);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiAL = datestr(datenum(datainiAL)-1,'yyyy-mm-dd');
                        else
                            DatiAnalogLink(indice,jj) = DatiAnalogLink(indice-1,jj);
                            ErrAnalogLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiAnalogLink(nnn,jj)));
                        if check == 1
                            DatiAnalogLink(nnn,jj) = DatiAnalogLink(nnn-1,jj);
                            ErrAnalogLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of Analog Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiAnalogLink = [DATiniAL str2double(DatiAnalogLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiAnalogLink(a,1) == DatiAnalogLink(a-1,1)
                        DatiAnalogLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
    end
    if NuovoZeroAL == 1 && yesAL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiAnalogLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiAnalogLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiAnalogLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiAnalogLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiAnalogLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
    else
        Dati = DatiAnalogLink;
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
end

%%  Crack Link
if  yesCrL == 0 % Non ci sono Crack Link
    DatiCrackLink = [];
    DatiNTCCrackLink = [];
    ErrCrackLink = [];
    ChCrL = [];
    CrL_NTC = [];
    wardat = 'lettura function: there are not Crack Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-Crack-RawData.csv']; % Specifico per Crack Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-NTCCrack-RawData.csv'];
    ChCrL = cell2mat(NodoCrackLink(:,4));
    CrL_NTC = cell2mat(NodoCrackLink(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(datainiCrL) == datenum(Data(1,1))
            tempoiniCrL = char(Data(1,2));
        end
        NodeType = 'Crack Link';
        NodeNumCrL = num2str(cell2mat(NodoCrackLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiCrL ''' and Time >= ''' tempoiniCrL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumCrL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniCrL = curs.Data;
        Ntempo = DATiniCrL;
        [r,c] = size(DATiniCrL);
        saltoCrL = 0;
        if r==1 && c==1
            textdatCrL = 'Warning: error reading Crack Link (control unit data)!';
            fprintf(fileID,fmt,textdatCrL);
            saltoCrL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiCrL ''' and UnitName = ''' IDcentralina ...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumCrL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2CrL = curs.Data;
        Ndate = DAT2CrL;
        if saltoCrL == 1
            DATiniCrL = DAT2CrL;
        end
        Check = size(DAT2CrL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoCrL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATiniCrL = [DATiniCrL; DAT2CrL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniCrL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniCrL(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniCrL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniCrL = T;
        else
            DATiniCrL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Extensometer Link
        if saltoCrL == 0
            if Check(1,2) > 1
                DatiCrackLink = cell(rd+rt,rCrL);
                DatiNTCCrackLink = cell(rd+rt,rCrL);
            else
                DatiCrackLink = cell(rt,rCrL);
                DatiNTCCrackLink = cell(rt,rCrL);
            end
        else
            DatiCrackLink = cell(rd,rCrL);
            DatiNTCCrackLink = cell(rd,rCrL);
        end
        NCrackLink = cell2mat(NodoCrackLink(:,2));
        col = 1;
        ii = 1;
        w = 0;
        while ii <= rCrL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NCrackLink(ii,1);
            nNodo = num2str(nN);
            % Dato
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datainiCrL ''' and Time >= ''' tempoiniCrL ''' and  ToolNameID = ''' ...
                DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoCrL = curs.Data;
            % Dato
            comando = ['select Val0 from RawDataView where Date > ''' ...
                datainiCrL ''' and UnitName = ''' IDcentralina ...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoCrLd = curs.Data;
            if ChCrL(ii,1) == 1
                % T
                DATnodoCrL_NTC = cell(1,1);
                DATnodoCrLd_NTC = cell(1,1);
            elseif ChCrL(ii,1) >= 2
                if CrL_NTC(ii,1)==1
                    comando = ['select Val1 from RawDataView where Date = ''' ...
                        datainiCrL ''' and Time >= ''' tempoiniCrL ''' and  ToolNameID = ''' ...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoCrL_NTC = curs.Data;
                    % Dato
                    comando = ['select Val1 from RawDataView where Date > ''' ...
                        datainiCrL ''' and UnitName = ''' IDcentralina ...
                        ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                        NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoCrLd_NTC = curs.Data;
                else
                    DATnodoCrL_NTC = cell(1,1);
                    DATnodoCrLd_NTC = cell(1,1);
                end
            end
            
            [rn,~] = size(DATnodoCrLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoCrLd(i,:) = DATnodoCrLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Crack Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            [rnT,~] = size(DATnodoCrLd_NTC); % controllo date
            if rnT < rd % normalmente sono uguali
                for i = rnT+1:rd
                    DATnodoCrLd_NTC(i,:) = DATnodoCrLd_NTC(rnT,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node Crack Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
                
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoCrL == 0
                if ChCrL(ii,1) >= 2
                    if CrL_NTC(ii,1)==1
                        DatiCrackLink(1:rt,col) = DATnodoCrL(:,:); %#ok<*AGROW>
                        DatiNTCCrackLink(1:rt,col) = DATnodoCrL_NTC(:,:); %#ok<*AGROW>
                        if Check(1,2) > 1
                            DatiCrackLink(rt+1:rt+rd,col) = DATnodoCrLd(:,:);
                            DatiNTCCrackLink(rt+1:rt+rd,col) = DATnodoCrLd_NTC(:,:); %#ok<*AGROW>
                        end
                    else
                        DatiCrackLink(1:rt,col) = DATnodoCrL(:,:); %#ok<*AGROW>
                        if Check(1,2) > 1
                            DatiCrackLink(rt+1:rt+rd,col) = DATnodoCrLd(:,:);
                        end
                    end
                elseif ChCrL(ii,1) == 1
                    DatiCrackLink(1:rt,col) = DATnodoCrL(:,:); %#ok<*AGROW>
                    if Check(1,2) > 1
                        DatiCrackLink(rt+1:rt+rd,col) = DATnodoCrLd(:,:);
                    end
                end                        
            else
                if ChCrL(ii,1) >= 2
                    if CrL_NTC(ii,1)==1
                        DatiCrackLink(1:rd,col) = DATnodoCrL(:,:); %#ok<*AGROW>
                        DatiNTCCrackLink(1:rd,col) = DATnodoCrL_NTC(:,:); %#ok<*AGROW>
                    else
                        DatiCrackLink(1:rd,col) = DATnodoCrL(:,:); %#ok<*AGROW>
                    end
                elseif ChCrL(ii,1) == 1
                    DatiCrackLink(1:rd,col) = DATnodoCrL(:,:); %#ok<*AGROW>
                end
            end
            
            okdatCrL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rCrL) ' Crack Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatCrL);
            ii = ii+1;
            col = col+1;
        end
        if w == 0
            wardat = 'There are not warning for Crack Link!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        
        % Dati
        [r,c] = size(DatiCrackLink);
        ErrCrackLink = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        NAN = 1;
        if NuovoZeroCrL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiCrL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
            % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',DatiCrackLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',DatiCrackLink(dis,jj)) == 0
                       DatiCrackLink = DatiCrackLink(dis:end,:);
                       DatiNTCCrackLink = DatiNTCCrackLink(dis:end,:);
                       DATiniCrL = DATiniCrL(dis:end,:);
                       [r,c] = size(DatiCrackLink);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(datainiCrL)+1 < now
                            datainiCrL = datestr(datenum(datainiCrL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesCrL = 0;
                        end
                    end
                end
                
            end
        end
        if DIS ~= 1 && yesCrL == 1
            % --- Codice per il primo dato ---
            for jj=1:c
                % Trovo l'indice della cella contenenti una determinata stringa.
                idxE = find(strcmp('Err1',DatiCrackLink(1,jj)));
                idxT = find(strcmp('---',DatiCrackLink(1,jj)));
                idxT2 = find(strcmp('-',DatiCrackLink(1,jj)));
                idxE2 = find(strcmp('Err2',DatiCrackLink(1,jj)));
                idxE255 = find(strcmp('Err255',DatiCrackLink(1,jj)));
                idxD = find(strcmp('DMUXe',DatiCrackLink(1,jj)));
                idxE1F = find(strcmp('Err1File',DatiCrackLink(1,jj)));
                idxN = find(strcmp('null',DatiCrackLink(1,jj)));
                idxRX = find(strcmp('No RX',DatiCrackLink(1,jj)));
                idxI = find(strcmp('id Error',DatiCrackLink(1,jj)));
                idxM = find(strcmp('MUX id Error',DatiCrackLink(1,jj)));
                idxC = find(strcmp('CH n. Error',DatiCrackLink(1,jj)));
                idxU = find(strcmp('Unknown Error',DatiCrackLink(1,jj)));
                idxMC = find(strcmp('MsgCorrup',DatiCrackLink(1,jj)));
                idxNS = find(strcmp('NotSupp',DatiCrackLink(1,jj)));
                idxU2 = find(strcmp('Unknown',DatiCrackLink(1,jj)));
                idxNA = find(strcmp('NotAv',DatiCrackLink(1,jj)));
                idxND = find(strcmp('No Data',DatiCrackLink(1,jj)));
                idxCE = find(strcmp('CH n. Er',DatiCrackLink(1,jj)));
                idx0 = find(strcmp('0.000',DatiCrackLink(1,jj)));
                idx = union(idxE,idxT);
                idx = union(idx,idxT2);
                idx = union(idx,idxCE);
                idx = union(idx,idxE2);
                idx = union(idx,idxE255);
                idx = union(idx,idxD);
                idx = union(idx,idxE1F);
                idx = union(idx,idxN);
                idx = union(idx,idxRX);
                idx = union(idx,idxI);
                idx = union(idx,idxM);
                idx = union(idx,idxC);
                idx = union(idx,idxU);
                idx = union(idx,idxMC);
                idx = union(idx,idxNS);
                idx = union(idx,idxU2);
                idx = union(idx,idxNA);
                idx = union(idx,idxND);
                idx = union(idx,idx0);
                [ri,~] = size(idx);
                if ri == 1
                    wardat = 'Warning on the correction of Crack Link nodes: there are not data at the beginning of the array!';
                    fprintf(fileID,fmt,wardat);
                    if rDR==1 && cDR==1 || NuovoZeroCrL == 0 % il file non è mai stato scritto, cerco la data precedente
                        NAN = 0;
                        ESCI = 1;
                        if datenum(datainiCrL)+1 < now
                            datainiCrL = datestr(datenum(datainiCrL)+1,'yyyy-mm-dd');
                            break
                        else
                            yesCrL = 0;
                        end
                    else
                        if isempty(RawDate) == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiCrL = datestr(datenum(datainiCrL)-1,'yyyy-mm-dd');
                            break
                        else
                            if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                DatiCrackLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                ErrCrackLink(1,jj) = 1;
                                wardat = 'Data of Crack Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            else
                                NAN = 0;
                                ESCI = 1;
                                datainiCrL = datestr(datenum(datainiCrL)-1,'yyyy-mm-dd');
                                break
                            end
                        end
                    end
                end
            end
            if ESCI == 0
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiCrackLink(:,jj)));
                    idxT = find(strcmp('---',DatiCrackLink(:,jj)));
                    idxT2 = find(strcmp('-',DatiCrackLink(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiCrackLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiCrackLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiCrackLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiCrackLink(:,jj)));
                    idxN = find(strcmp('null',DatiCrackLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiCrackLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiCrackLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiCrackLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiCrackLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiCrackLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiCrackLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiCrackLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiCrackLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiCrackLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiCrackLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiCrackLink(:,jj)));
                    idx0 = find(strcmp('0.000',DatiCrackLink(:,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    idx = union(idx,idx0);
                    [ri,~] = size(idx);
                    for kk = 1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            datainiCrL = datestr(datenum(datainiCrL)+1,'yyyy-mm-dd');
                            break
                        else
                            DatiCrackLink(indice,jj) = DatiCrackLink(indice-1,jj);
                            ErrCrackLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiCrackLink(nnn,jj)));
                        if check == 1
                            DatiCrackLink(nnn,jj) = DatiCrackLink(nnn-1,jj);
                            ErrCrackLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of Crack Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiCrackLink = [DATiniCrL str2double(DatiCrackLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiCrackLink(a,1) == DatiCrackLink(a-1,1)
                        DatiCrackLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            end
        end
            
        if ESCI == 0
            if sum(CrL_NTC(:,1))>0
                if DIS ~= 1 && yesCrL == 1
                    % NTC
                    [r,c] = size(DatiNTCCrackLink);
                    Ncorr = 0;
                    if NuovoZeroCrL == 1
                        if isfile(RawDataFile) == 1
                            DatiRawNTC = csvread(RawDataFileNTC);
                            [rDR,cDR] = size(DatiRawNTC);
                            DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                            RawDateNTC = find(DatiRawNTC(:,1)<=datenum(datainiCrL));
                        else
                            rDR = 1;
                            cDR = 1;
                            RawDateNTC = [];
                            DatiRawNTC = [];
                        end
                    else
                        rDR = 1;
                        cDR = 1;
                        RawDateNTC = [];
                        DatiRawNTC = [];
                    end
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTCCrackLink(1,jj)));
                        idxND = find(strcmp('No Data',DatiNTCCrackLink(1,jj)));
                        idxT3 = find(strcmp('---',DatiNTCCrackLink(1,jj)));
                        idxT = find(strcmp('-',DatiNTCCrackLink(1,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTCCrackLink(1,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTCCrackLink(1,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTCCrackLink(1,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTCCrackLink(1,jj)));
                        idxN = find(strcmp('null',DatiNTCCrackLink(1,jj)));
                        idxRX = find(strcmp('No RX',DatiNTCCrackLink(1,jj)));
                        idxI = find(strcmp('id Error',DatiNTCCrackLink(1,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTCCrackLink(1,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTCCrackLink(1,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTCCrackLink(1,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTCCrackLink(1,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTCCrackLink(1,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTCCrackLink(1,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTCCrackLink(1,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTCCrackLink(1,jj)));
                        idx = union(idxE,idxT3);
                        idx = union(idx,idxND);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxT);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        [ri,~] = size(idx);
                        if ri == 1
                            wardat = 'Warning on the correction of NTC of Crack Link nodes: there are not data at the beginning of the array!';
                            fprintf(fileID,fmt,wardat);
                            if rDR==1 && cDR==1 || NuovoZeroCrL == 0 % il file non è mai stato scritto, cerco la data precedente
                                NAN = 0;
                                ESCI = 1;
                                if datenum(datainiCrL)+1 < now
                                    datainiCrL = datestr(datenum(datainiCrL)+1,'yyyy-mm-dd');
                                    break
                                else
                                    yesCrL = 0;
                                end
                            else
                                if isempty(RawDateNTC) == 1
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiCrL = datestr(datenum(datainiCrL)-1,'yyyy-mm-dd');
                                    break
                                else
                                    DatiNTCCrackLink(1,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                    if ErrCrackLink(1,jj) == 0
                                        ErrCrackLink(1,jj) = 0.5;
                                    else
                                        ErrCrackLink(1,jj) = 1;
                                    end
                                    wardat = 'Data of NTC of Crack Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                end
                            end
                        end
                    end
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiNTCCrackLink(:,jj)));
                        idxND = find(strcmp('No Data',DatiNTCCrackLink(:,jj)));
                        idxT3 = find(strcmp('---',DatiNTCCrackLink(:,jj)));
                        idxT = find(strcmp('-',DatiNTCCrackLink(:,jj)));
                        idxE2 = find(strcmp('Err2',DatiNTCCrackLink(:,jj)));
                        idxE255 = find(strcmp('Err255',DatiNTCCrackLink(:,jj)));
                        idxD = find(strcmp('DMUXe',DatiNTCCrackLink(:,jj)));
                        idxE1F = find(strcmp('Err1File',DatiNTCCrackLink(:,jj)));
                        idxN = find(strcmp('null',DatiNTCCrackLink(:,jj)));
                        idxRX = find(strcmp('No RX',DatiNTCCrackLink(:,jj)));
                        idxI = find(strcmp('id Error',DatiNTCCrackLink(:,jj)));
                        idxM = find(strcmp('MUX id Error',DatiNTCCrackLink(:,jj)));
                        idxC = find(strcmp('CH n. Error',DatiNTCCrackLink(:,jj)));
                        idxU = find(strcmp('Unknown Error',DatiNTCCrackLink(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiNTCCrackLink(:,jj)));
                        idxNS = find(strcmp('NotSupp',DatiNTCCrackLink(:,jj)));
                        idxU2 = find(strcmp('Unknown',DatiNTCCrackLink(:,jj)));
                        idxNA = find(strcmp('NotAv',DatiNTCCrackLink(:,jj)));
                        idxCE = find(strcmp('CH n. Er',DatiNTCCrackLink(:,jj)));
                        idx = union(idxE,idxT3);
                        idx = union(idx,idxND);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxT);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                if DIS == 1
                                    wardat = 'Warning on the correction of NTC of Crack Link: there are not data at the beginning of the array!';
                                    fprintf(fileID,fmt,wardat);
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiCrL = datestr(datenum(datainiCrL)+1,'yyyy-mm-dd');
                                    break
                                else
                                    wardat = 'Warning on the correction of NTC of Crack Link nodes: there are not data at the beginning of the array!';
                                    fprintf(fileID,fmt,wardat);
                                    if rDR==1 && cDR==1 % il file non è mai stato scritto
                                        NAN = 0;
                                        ESCI = 1;
                                        datainiCrL = datestr(datenum(datainiCrL)-1,'yyyy-mm-dd');
                                        break
                                    else
                                        if isempty(RawDateNTC) == 1
                                            NAN = 0;
                                            ESCI = 1;
                                            datainiCrL = datestr(datenum(datainiCrL)-1,'yyyy-mm-dd');
                                            break
                                        else
                                            DatiNTCCrackLink(indice,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                            if ErrCrackLink(indice,jj) == 0
                                                ErrCrackLink(indice,jj) = 0.5;
                                            else
                                                ErrCrackLink(indice,jj) = 1;
                                            end
                                            wardat = 'Data of Crack Link nodes corrected using Raw Data of Excel file.';
                                            fprintf(fileID,fmt,wardat);
                                            Ncorr = Ncorr+ri;
                                            NAN = 1;
                                        end
                                    end
                                end
                            else
                                DatiNTCCrackLink(indice,jj) = DatiNTCCrackLink(indice-1,jj);
                                if ErrCrackLink(indice,jj) == 0
                                    ErrCrackLink(indice,jj) = 0.5;
                                else
                                    ErrCrackLink(indice,jj) = 1;
                                end
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(DatiNTCCrackLink(nnn,jj)));
                            if check == 1
                                DatiNTCCrackLink(nnn,jj) = DatiNTCCrackLink(nnn-1,jj);
                                if ErrCrackLink(indice,jj) == 0
                                    ErrCrackLink(indice,jj) = 0.5;
                                else
                                    ErrCrackLink(indice,jj) = 1;
                                end
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of NTC Crack Link data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    DatiNTCCrackLink = [DATiniCrL str2double(DatiNTCCrackLink)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if DatiNTCCrackLink(a,1) == DatiNTCCrackLink(a-1,1)
                            DatiNTCCrackLink(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            end
        end
    end
    if NuovoZeroCrL == 1 && yesCrL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiCrackLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiCrackLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiCrackLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiCrackLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiCrackLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end

        if sum(CrL_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTCCrackLink(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTCCrackLink(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTCCrackLink(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTCCrackLink(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTCCrackLink;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = DatiCrackLink;
        if sum(CrL_NTC(:,1))>0
            DatiNTC = DatiNTCCrackLink;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(CrL_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
end

%%  3D Crack Link
if  yes3DCrL == 0 % Non ci sono 3D Crack Link
    Dati3DCrackLink = [];
    DatiNTC3DCrackLink = [];
    Err3DCrackLink = [];
    ErrNTC3DCrackLink = [];
    Ch3DCrL = [];
    CrL3D_NTC = [];
    wardat = 'lettura function: there are not 3D Crack Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-3DCrack-RawData.csv']; % Specifico per 3D Crack Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-3DCrackNTC-RawData.csv']; % Specifico per 3D Crack Link
    Ch3DCrL = cell2mat(Nodo3DCrackLink(:,4));
    CrL3D_NTC = cell2mat(Nodo3DCrackLink(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(dataini3DCrL) == datenum(Data(1,1))
            tempoini3DCrL = char(Data(1,2));
        end
        NodeType = '3D Crack Link';
        NodeNum3DCrL = num2str(cell2mat(Nodo3DCrackLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            dataini3DCrL ''' and Time >= ''' tempoini3DCrL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNum3DCrL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATini3DCrL = curs.Data;
        Ntempo = DATini3DCrL;
        [r,c] = size(DATini3DCrL);
        salto3DCrL = 0;
        if r==1 && c==1
            textdat3DCrL = 'Warning: error reading 3D Crack Link (control unit data)!';
            fprintf(fileID,fmt,textdat3DCrL);
            salto3DCrL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            dataini3DCrL ''' and UnitName = ''' IDcentralina...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNum3DCrL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT23DCrL = curs.Data;
        Ndate = DAT23DCrL;
        if salto3DCrL == 1
            DATini3DCrL = DAT23DCrL;
        end
        Check = size(DAT23DCrL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if salto3DCrL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATini3DCrL = [DATini3DCrL; DAT23DCrL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATini3DCrL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATini3DCrL(:,1)) repmat(' ', [rD,1]) cell2mat(DATini3DCrL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATini3DCrL = T;
        else
            DATini3DCrL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        if salto3DCrL == 0
            % creo la matrice per i dati 3D Crack Link
            if Check(1,2) > 1
                Dati3DCrackLink = cell(rd+rt,3*r3DCrL); 
                DatiNTC3DCrackLink = cell(rd+rt,r3DCrL);
            else
                Dati3DCrackLink = cell(rt,3*r3DCrL);
                DatiNTC3DCrackLink = cell(rt,r3DCrL);
            end
        else
            Dati3DCrackLink = cell(rd,3*r3DCrL);
            DatiNTC3DCrackLink = cell(rd,r3DCrL);
        end
        col = 1;
        ii = 1;
        w = 0;
        N3DCrackLink = cell2mat(Nodo3DCrackLink(:,2));
        while ii <= r3DCrL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = N3DCrackLink(ii,1);
            nNodo = num2str(nN);
            if Ch3DCrL(ii,1) == 3 || Ch3DCrL(ii,1) == 4
                comando = ['select Val0, Val1, Val2 from RawDataView where Date = ''' ...
                    dataini3DCrL ''' and Time >= ''' tempoini3DCrL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo3DCrL = curs.Data;
                comando = ['select Val0, Val1, Val2 from RawDataView where Date > ''' ...
                    dataini3DCrL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo3DCrLd = curs.Data;
                if Ch3DCrL(ii,1) == 4 % Termometro
                    comando = ['select Val3 from RawDataView where Date = ''' ...
                        dataini3DCrL ''' and Time >= ''' tempoini3DCrL ''' and UnitName = ''' IDcentralina...
                        ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodo3DCrL_NTC = curs.Data;
                    comando = ['select Val3 from RawDataView where Date > ''' ...
                        dataini3DCrL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodo3DCrL_NTCd = curs.Data;
                else
                    DATnodo3DCrL_NTC = 0;
                    DATnodo3DCrL_NTCd = 0;
                end
            elseif Ch3DCrL(ii,1) >= 6
                comando = ['select Val0, Val2, Val4 from RawDataView where Date = ''' ...
                    dataini3DCrL ''' and Time >= ''' tempoini3DCrL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo3DCrL = curs.Data;
                comando = ['select Val0, Val2, Val4 from RawDataView where Date > ''' ...
                    dataini3DCrL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo3DCrLd = curs.Data;
                DATnodo3DCrL_NTC = 0;
                DATnodo3DCrL_NTCd = 0;
            end
            
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            [rn,~] = size(DATnodo3DCrLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodo3DCrLd(i,:) = DATnodo3DCrLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node 3D Crack Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            [rn,~] = size(DATnodo3DCrL_NTCd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodo3DCrL_NTCd(i,:) = DATnodo3DCrL_NTCd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node 3D Crack Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if salto3DCrL == 0
                if Ch3DCrL(ii,1) == 4
                    if CrL3D_NTC(ii,1) == 1
                        Dati3DCrackLink(1:rt,col:col+2) = DATnodo3DCrL; %#ok<*AGROW>
                        DatiNTC3DCrackLink(1:rt,ii) = DATnodo3DCrL_NTC; %#ok<*AGROW>
                        if Check(1,2) > 1
                            Dati3DCrackLink(rt+1:rt+rd,col:col+2) = DATnodo3DCrLd;
                            DatiNTC3DCrackLink(rt+1:rt+rd,ii) = DATnodo3DCrL_NTCd; %#ok<*AGROW>
                        end
                    else
                        Dati3DCrackLink(1:rt,col:col+2) = DATnodo3DCrL; %#ok<*AGROW>
                        if Check(1,2) > 1
                            Dati3DCrackLink(rt+1:rt+rd,col:col+2) = DATnodo3DCrLd;
                        end
                    end
                elseif Ch3DCrL(ii,1) == 3 || Ch3DCrL(ii,1) >= 6
                    Dati3DCrackLink(1:rt,col:col+2) = DATnodo3DCrL; %#ok<*AGROW>
                    if Check(1,2) > 1
                        Dati3DCrackLink(rt+1:rt+rd,col:col+2) = DATnodo3DCrLd;
                    end
                end
            else
                if Ch3DCrL(ii,1) == 4
                    if CrL3D_NTC(ii,1)==1
                        Dati3DCrackLink(1:rd,col:col+2) = DATnodo3DCrL; %#ok<*AGROW>
                        DatiNTC3DCrackLink(1:rd,ii) = DATnodo3DCrL_NTC; %#ok<*AGROW>
                    else
                        Dati3DCrackLink(1:rd,col:col+2) = DATnodo3DCrL; %#ok<*AGROW>
                    end
                elseif Ch3DCrL(ii,1) == 1
                    Dati3DCrackLink(1:rd,col:col+2) = DATnodo3DCrL; %#ok<*AGROW>
                end
            end
            
            okdat3DCrL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(r3DCrL) ' 3D Crack Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdat3DCrL);
            ii = ii+1;
            col = col+3;
        end
        if w == 0
            wardat = 'There are not warning for 3D Crack Link!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(Dati3DCrackLink);
        Err3DCrackLink = zeros(r,c);
        Ncorr = 0;
        NAN = 1;
        ESCI = 0;
        if NuovoZero3DCrL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(dataini3DCrL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
             % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',Dati3DCrackLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',Dati3DCrackLink(dis,jj)) == 0
                       Dati3DCrackLink = Dati3DCrackLink(dis:end,:);
                       DatiNTC3DCrackLink = DatiNTC3DCrackLink(dis:end,:);
                       DATini3DCrL = DATini3DCrL(dis:end,:);
                       [r,c] = size(Dati3DCrackLink);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(dataini3DCrL)+1 < now
                            dataini3DCrL = datestr(datenum(dataini3DCrL)+1,'yyyy-mm-dd');
                            break
                        else
                            yes3DCrL = 0;
                        end
                    end
                end
            end
        end
        if NAN == 1
            if DIS ~= 1 && yes3DCrL == 1
                % --- Codice per il primo dato ---
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',Dati3DCrackLink(1,jj)));
                    idxT = find(strcmp('---',Dati3DCrackLink(1,jj)));
                    idxT2 = find(strcmp('-',Dati3DCrackLink(1,jj)));
                    idxE2 = find(strcmp('Err2',Dati3DCrackLink(1,jj)));
                    idxE255 = find(strcmp('Err255',Dati3DCrackLink(1,jj)));
                    idxD = find(strcmp('DMUXe',Dati3DCrackLink(1,jj)));
                    idxE1F = find(strcmp('Err1File',Dati3DCrackLink(1,jj)));
                    idxN = find(strcmp('null',Dati3DCrackLink(1,jj)));
                    idxRX = find(strcmp('No RX',Dati3DCrackLink(1,jj)));
                    idxI = find(strcmp('id Error',Dati3DCrackLink(1,jj)));
                    idxM = find(strcmp('MUX id Error',Dati3DCrackLink(1,jj)));
                    idxC = find(strcmp('CH n. Error',Dati3DCrackLink(1,jj)));
                    idxU = find(strcmp('Unknown Error',Dati3DCrackLink(1,jj)));
                    idxMC = find(strcmp('MsgCorrup',Dati3DCrackLink(1,jj)));
                    idxNS = find(strcmp('NotSupp',Dati3DCrackLink(1,jj)));
                    idxU2 = find(strcmp('Unknown',Dati3DCrackLink(1,jj)));
                    idxNA = find(strcmp('NotAv',Dati3DCrackLink(1,jj)));
                    idxND = find(strcmp('No Data',Dati3DCrackLink(1,jj)));
                    idxCE = find(strcmp('CH n. Er',Dati3DCrackLink(1,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    if ri == 1
                        wardat = 'Warning on the correction of 3D Crack Link nodes: there are not data at the beginning of the array!';
                        fprintf(fileID,fmt,wardat);
                        if rDR==1 && cDR==1 || NuovoZero3DCrL == 0 % il file non è mai stato scritto, cerco la data precedente
                            NAN = 0;
                            ESCI = 1;
                            if datenum(dataini3DCrL)+1 < now
                                dataini3DCrL = datestr(datenum(dataini3DCrL)+1,'yyyy-mm-dd');
                                break
                            else
                                yes3DCrL = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini3DCrL = datestr(datenum(dataini3DCrL)-1,'yyyy-mm-dd');
                                break
                            else
                                if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                    Dati3DCrackLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                    Err3DCrackLink(1,jj) = 1;
                                    wardat = 'Data of 3D Crack Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                else
                                    NAN = 0;
                                    ESCI = 1;
                                    dataini3DCrL = datestr(datenum(dataini3DCrL)-1,'yyyy-mm-dd');
                                    break
                                end
                            end
                        end
                    end
                end
                if ESCI == 0
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',Dati3DCrackLink(:,jj)));
                        idxT = find(strcmp('---',Dati3DCrackLink(:,jj)));
                        idxT2 = find(strcmp('-',Dati3DCrackLink(:,jj)));
                        idxE2 = find(strcmp('Err2',Dati3DCrackLink(:,jj)));
                        idxE255 = find(strcmp('Err255',Dati3DCrackLink(:,jj)));
                        idxD = find(strcmp('DMUXe',Dati3DCrackLink(:,jj)));
                        idxE1F = find(strcmp('Err1File',Dati3DCrackLink(:,jj)));
                        idxN = find(strcmp('null',Dati3DCrackLink(:,jj)));
                        idxRX = find(strcmp('No RX',Dati3DCrackLink(:,jj)));
                        idxI = find(strcmp('id Error',Dati3DCrackLink(:,jj)));
                        idxM = find(strcmp('MUX id Error',Dati3DCrackLink(:,jj)));
                        idxC = find(strcmp('CH n. Error',Dati3DCrackLink(:,jj)));
                        idxU = find(strcmp('Unknown Error',Dati3DCrackLink(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',Dati3DCrackLink(:,jj)));
                        idxNS = find(strcmp('NotSupp',Dati3DCrackLink(:,jj)));
                        idxU2 = find(strcmp('Unknown',Dati3DCrackLink(:,jj)));
                        idxNA = find(strcmp('NotAv',Dati3DCrackLink(:,jj)));
                        idxND = find(strcmp('No Data',Dati3DCrackLink(:,jj)));
                        idxCE = find(strcmp('CH n. Er',Dati3DCrackLink(:,jj)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxT2);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxND);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini3DCrL = datestr(datenum(dataini3DCrL)-1,'yyyy-mm-dd');
                                break
                            else
                                Dati3DCrackLink(indice,jj) = Dati3DCrackLink(indice-1,jj);
                                Err3DCrackLink(indice,jj) = 1;
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(Dati3DCrackLink(nnn,jj)));
                            if check == 1
                                Dati3DCrackLink(nnn,jj) = Dati3DCrackLink(nnn-1,jj);
                                Err3DCrackLink(nnn,jj) = 1;
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of 3D Crack Link data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    Dati3DCrackLink = [DATini3DCrL str2double(Dati3DCrackLink)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if Dati3DCrackLink(a,1) == Dati3DCrackLink(a-1,1)
                            Dati3DCrackLink(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            end
        end
        
        if ESCI == 0
            if sum(CrL3D_NTC(:,1))>0
                % NTC
                % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
                %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
                [r,c] = size(DatiNTC3DCrackLink);
                Ncorr = 0;
                ErrNTC3DCrackLink = zeros(r,c);
                if NuovoZero3DCrL == 1
                    if isfile(RawDataFile) == 1
                        DatiRawNTC = csvread(RawDataFileNTC);
                        [rDR,cDR] = size(DatiRawNTC);
                        DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                        RawDateNTC = find(DatiRawNTC(:,1)<=datenum(dataini3DCrL));
                    else
                        rDR = 1;
                        cDR = 1;
                        RawDateNTC = [];
                        DatiRawNTC = [];
                    end
                else
                    rDR = 1;
                    cDR = 1;
                    RawDateNTC = [];
                    DatiRawNTC = [];
                end
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiNTC3DCrackLink(1,jj)));
                    idxT3 = find(strcmp('---',DatiNTC3DCrackLink(1,jj)));
                    idxT = find(strcmp('-',DatiNTC3DCrackLink(1,jj)));
                    idxE2 = find(strcmp('Err2',DatiNTC3DCrackLink(1,jj)));
                    idxE255 = find(strcmp('Err255',DatiNTC3DCrackLink(1,jj)));
                    idxD = find(strcmp('DMUXe',DatiNTC3DCrackLink(1,jj)));
                    idxE1F = find(strcmp('Err1File',DatiNTC3DCrackLink(1,jj)));
                    idxN = find(strcmp('null',DatiNTC3DCrackLink(1,jj)));
                    idxRX = find(strcmp('No RX',DatiNTC3DCrackLink(1,jj)));
                    idxI = find(strcmp('id Error',DatiNTC3DCrackLink(1,jj)));
                    idxM = find(strcmp('MUX id Error',DatiNTC3DCrackLink(1,jj)));
                    idxC = find(strcmp('CH n. Error',DatiNTC3DCrackLink(1,jj)));
                    idxU = find(strcmp('Unknown Error',DatiNTC3DCrackLink(1,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiNTC3DCrackLink(1,jj)));
                    idxNS = find(strcmp('NotSupp',DatiNTC3DCrackLink(1,jj)));
                    idxU2 = find(strcmp('Unknown',DatiNTC3DCrackLink(1,jj)));
                    idxNA = find(strcmp('NotAv',DatiNTC3DCrackLink(1,jj)));
                    idxND = find(strcmp('No Data',DatiNTC3DCrackLink(1,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiNTC3DCrackLink(1,jj)));
                    idx = union(idxE,idxT3);
                    idx = union(idx,idxND);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxT);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    [ri,~] = size(idx);
                    if ri == 1
                        wardat = 'Warning on the correction of 3D Crack Link nodes: there are not data at the beginning of the array!';
                        fprintf(fileID,fmt,wardat);
                        if rDR==1 && cDR==1 || NuovoZero3DCrL == 0 % il file non è mai stato scritto, cerco la data precedente
                            NAN = 0;
                            ESCI = 1;
                            if datenum(dataini3DCrL)+1 < now
                                dataini3DCrL = datestr(datenum(dataini3DCrL)+1,'yyyy-mm-dd');
                                break
                            else
                                yes3DCrL = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini3DCrL = datestr(datenum(dataini3DCrL)-1,'yyyy-mm-dd');
                                break
                            else
                                Dati3DCrackLink(1,jj) = cellstr(num2str(DatiRaw(RawDateNTC(end),jj+1)));
                                Err3DCrackLink(1,jj) = 1;
                                wardat = 'Data of 3D Crack Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                    end
                end
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiNTC3DCrackLink(:,jj)));
                    idxT3 = find(strcmp('---',DatiNTC3DCrackLink(:,jj)));
                    idxT = find(strcmp('-',DatiNTC3DCrackLink(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiNTC3DCrackLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiNTC3DCrackLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiNTC3DCrackLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiNTC3DCrackLink(:,jj)));
                    idxN = find(strcmp('null',DatiNTC3DCrackLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiNTC3DCrackLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiNTC3DCrackLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiNTC3DCrackLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiNTC3DCrackLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiNTC3DCrackLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiNTC3DCrackLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiNTC3DCrackLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiNTC3DCrackLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiNTC3DCrackLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiNTC3DCrackLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiNTC3DCrackLink(:,jj)));
                    idx = union(idxE,idxT3);
                    idx = union(idx,idxND);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxT);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            dataini3DCrL = datestr(datenum(dataini3DCrL)-1,'yyyy-mm-dd');
                        else
                            DatiNTC3DCrackLink(indice,jj) = DatiNTC3DCrackLink(indice-1,jj);
                            ErrNTC3DCrackLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiNTC3DCrackLink(nnn,jj)));
                        if check == 1
                            DatiNTC3DCrackLink(nnn,jj) = DatiNTC3DCrackLink(nnn-1,jj);
                            ErrNTC3DCrackLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of NTC of 3D Crack Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiNTC3DCrackLink = [DATini3DCrL str2double(DatiNTC3DCrackLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiNTC3DCrackLink(a,1) == DatiNTC3DCrackLink(a-1,1)
                        DatiNTC3DCrackLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            else
                ErrNTC3DCrackLink = 0;
            end
        end
    end
    if NuovoZero3DCrL == 1 && yes3DCrL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=Dati3DCrackLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(Dati3DCrackLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(Dati3DCrackLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);Dati3DCrackLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = Dati3DCrackLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
        
        if sum(CrL3D_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTC3DCrackLink(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTC3DCrackLink(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTC3DCrackLink(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTC3DCrackLink(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTC3DCrackLink;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = Dati3DCrackLink;
        if sum(CrL3D_NTC(:,1))>0
            DatiNTC = DatiNTC3DCrackLink;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(CrL3D_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
end

%%  2D Crack Link
if  yes2DCrL == 0 % Non ci sono 2D Crack Link
    Dati2DCrackLink = [];
    DatiNTC2DCrackLink = [];
    Err2DCrackLink = [];
    ErrNTC2DCrackLink = [];
    Ch2DCrL = [];
    CrL2D_NTC = [];
    wardat = 'lettura function: there are not 2D Crack Link';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-2DCrack-RawData.csv']; % Specifico per 2D Crack Link
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-2DCrackNTC-RawData.csv']; % Specifico per 2D Crack Link
    Ch2DCrL = cell2mat(Nodo2DCrackLink(:,4));
    CrL2D_NTC = cell2mat(Nodo2DCrackLink(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(dataini2DCrL) == datenum(Data(1,1))
            tempoini2DCrL = char(Data(1,2));
        end
        NodeType = '2D Crack Link';
        NodeNum2DCrL = num2str(cell2mat(Nodo2DCrackLink(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            dataini2DCrL ''' and Time >= ''' tempoini2DCrL ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNum2DCrL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATini2DCrL = curs.Data;
        Ntempo = DATini2DCrL;
        [r,c] = size(DATini2DCrL);
        salto2DCrL = 0;
        if r==1 && c==1
            textdat2DCrL = 'Warning: error reading 2D Crack Link (control unit data)!';
            fprintf(fileID,fmt,textdat2DCrL);
            salto2DCrL = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            dataini2DCrL ''' and UnitName = ''' IDcentralina...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNum2DCrL ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT22DCrL = curs.Data;
        Ndate = DAT22DCrL;
        if salto2DCrL == 1
            DATini2DCrL = DAT22DCrL;
        end
        Check = size(DAT22DCrL);
        if Check(1,2) > 1 % ho superato il primo giorno
            if salto2DCrL == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATini2DCrL = [DATini2DCrL; DAT22DCrL];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATini2DCrL);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATini2DCrL(:,1)) repmat(' ', [rD,1]) cell2mat(DATini2DCrL(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATini2DCrL = T;
        else
            DATini2DCrL = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        if salto2DCrL == 0
            % creo la matrice per i dati 2D Crack Link
            if Check(1,2) > 1
                Dati2DCrackLink = cell(rd+rt,2*r2DCrL); 
                DatiNTC2DCrackLink = cell(rd+rt,r2DCrL);
            else
                Dati2DCrackLink = cell(rt,2*r2DCrL);
                DatiNTC2DCrackLink = cell(rt,r2DCrL);
            end
        else
            Dati2DCrackLink = cell(rd,2*r2DCrL);
            DatiNTC2DCrackLink = cell(rd,r2DCrL);
        end
        col = 1;
        ii = 1;
        w = 0;
        N2DCrackLink = cell2mat(Nodo2DCrackLink(:,2));
        Ch2DCrL = cell2mat(Ch2DCrL);
        while ii <= r2DCrL
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = N2DCrackLink(ii,1);
            nNodo = num2str(nN);
            if Ch2DCrL(ii,1) == 2 || Ch2DCrL(ii,1) == 3 
                comando = ['select Val0, Val1 from RawDataView where Date = ''' ...
                    dataini2DCrL ''' and Time >= ''' tempoini2DCrL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo2DCrL = curs.Data;
                comando = ['select Val0, Val1 from RawDataView where Date > ''' ...
                    dataini2DCrL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo2DCrLd = curs.Data;
                if Ch2DCrL(ii,1) == 3 % Termometro
                    comando = ['select Val2 from RawDataView where Date = ''' ...
                        dataini2DCrL ''' and Time >= ''' tempoini2DCrL ''' and UnitName = ''' IDcentralina...
                        ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodo2DCrL_NTC = curs.Data;
                    comando = ['select Val2 from RawDataView where Date > ''' ...
                        dataini2DCrL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodo2DCrL_NTCd = curs.Data;
                else
                    DATnodo2DCrL_NTC = 0;
                    DATnodo2DCrL_NTCd = 0;
                end
            elseif Ch2DCrL(ii,1) >= 4
                comando = ['select Val0, Val2 from RawDataView where Date = ''' ...
                    dataini32CrL ''' and Time >= ''' tempoini2DCrL ''' and UnitName = ''' IDcentralina...
                    ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum ='  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo2DCrL = curs.Data;
                comando = ['select Val0, Val2 from RawDataView where Date > ''' ...
                    dataini2DCrL ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' ...
                    NodeType ''' and NodeNum = '  nNodo ''];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodo2DCrLd = curs.Data;
                DATnodo2DCrL_NTC = 0;
                DATnodo2DCrL_NTCd = 0;
            end
            
            % Concateno i dati relativi a Step 1 e 2 per il nodo ii-esimo.
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            [rn,~] = size(DATnodo2DCrLd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodo2DCrLd(i,:) = DATnodo2DCrLd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node 2D Crack Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            [rn,~] = size(DATnodo2DCrL_NTCd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodo2DCrL_NTCd(i,:) = DATnodo2DCrL_NTCd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node 2D Crack Link ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            if salto2DCrL == 0
                if Ch2DCrL(ii,1) == 3
                    if CrL2D_NTC(ii,1) == 1
                        Dati2DCrackLink(1:rt,col:col+1) = DATnodo2DCrL; %#ok<*AGROW>
                        Dati2DCrackLink_NTC(1:rt,ii) = DATnodo2DCrL_NTC; %#ok<*AGROW>
                        if Check(1,2) > 1
                            Dati2DCrackLink(rt+1:rt+rd,col:col+1) = DATnodo2DCrLd;
                            Dati2DCrackLink_NTC(rt+1:rt+rd,ii) = DATnodo2DCrL_NTCd; %#ok<*AGROW>
                        end
                    else
                        Dati2DCrackLink(1:rt,col:col+1) = DATnodo2DCrL; %#ok<*AGROW>
                        if Check(1,2) > 1
                            Dati2DCrackLink(rt+1:rt+rd,col:col+1) = DATnodo3DCrLd;
                        end
                    end
                elseif Ch3DCrL(ii,1) == 2 || Ch3DCrL(ii,1) >= 4
                    Dati2DCrackLink(1:rt,col:col+1) = DATnodo2DCrL; %#ok<*AGROW>
                    if Check(1,2) > 1
                        Dati2DCrackLink(rt+1:rt+rd,col:col+1) = DATnodo2DCrLd;
                    end
                end
            else
                if Ch2DCrL(ii,1) == 3
                    if CrL2D_NTC(ii,1)==1
                        Dati2DCrackLink(1:rd,col:col+1) = DATnodo2DCrL; %#ok<*AGROW>
                        Dati2DCrackLink_NTC(1:rd,ii) = DATnodo2DCrL_NTC; %#ok<*AGROW>
                    else
                        Dati2DCrackLink(1:rd,col:col+1) = DATnodo2DCrL; %#ok<*AGROW>
                    end
                elseif Ch2DCrL(ii,1) == 1
                    Dati2DCrackLink(1:rd,col:col+1) = DATnodo2DCrL; %#ok<*AGROW>
                end
            end
            
            okdat2DCrL = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(r2DCrL) ' 2D Crack Link nodes downloaded correctly'];
            fprintf(fileID,fmt,okdat2DCrL);
            ii = ii+1;
            col = col+2;
        end
        if w == 0
            wardat = 'There are not warning for 2D Crack Link!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        [r,c] = size(Dati2DCrackLink);
        Err2DCrackLink = zeros(r,c);
        Ncorr = 0;
        NAN = 1;
        ESCI = 0;
        if NuovoZero2DCrL == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(dataini2DCrL));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        for jj=1:c
             % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
            % è ancora stato installato!)
            if strcmp('Dis.',Dati2DCrackLink(1,jj)) == 1
                for dis = 2:r
                    if strcmp('Dis.',Dati2DCrackLink(dis,jj)) == 0
                       Dati2DCrackLink = Dati2DCrackLink(dis:end,:);
                       DatiNTC2DCrackLink = DatiNTC2DCrackLink(dis:end,:);
                       DATini2DCrL = DATini2DCrL(dis:end,:);
                       [r,c] = size(Dati2DCrackLink);
                       break
                    elseif dis == r
                        NAN = 0;
                        DIS = 1;
                        if datenum(dataini2DCrL)+1 < now
                            dataini2DCrL = datestr(datenum(dataini2DCrL)+1,'yyyy-mm-dd');
                            break
                        else
                            yes2DCrL = 0;
                        end
                    end
                end
            end
        end
        if NAN == 1
            if DIS ~= 1 && yes2DCrL == 1
                % --- Codice per il primo dato ---
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',Dati2DCrackLink(1,jj)));
                    idxT = find(strcmp('---',Dati2DCrackLink(1,jj)));
                    idxT2 = find(strcmp('-',Dati2DCrackLink(1,jj)));
                    idxE2 = find(strcmp('Err2',Dati2DCrackLink(1,jj)));
                    idxE255 = find(strcmp('Err255',Dati2DCrackLink(1,jj)));
                    idxD = find(strcmp('DMUXe',Dati2DCrackLink(1,jj)));
                    idxE1F = find(strcmp('Err1File',Dati2DCrackLink(1,jj)));
                    idxN = find(strcmp('null',Dati2DCrackLink(1,jj)));
                    idxRX = find(strcmp('No RX',Dati2DCrackLink(1,jj)));
                    idxI = find(strcmp('id Error',Dati2DCrackLink(1,jj)));
                    idxM = find(strcmp('MUX id Error',Dati2DCrackLink(1,jj)));
                    idxC = find(strcmp('CH n. Error',Dati2DCrackLink(1,jj)));
                    idxU = find(strcmp('Unknown Error',Dati2DCrackLink(1,jj)));
                    idxMC = find(strcmp('MsgCorrup',Dati2DCrackLink(1,jj)));
                    idxNS = find(strcmp('NotSupp',Dati2DCrackLink(1,jj)));
                    idxU2 = find(strcmp('Unknown',Dati2DCrackLink(1,jj)));
                    idxNA = find(strcmp('NotAv',Dati2DCrackLink(1,jj)));
                    idxND = find(strcmp('No Data',Dati2DCrackLink(1,jj)));
                    idxCE = find(strcmp('CH n. Er',Dati2DCrackLink(1,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    if ri == 1
                        wardat = 'Warning on the correction of 2D Crack Link nodes: there are not data at the beginning of the array!';
                        fprintf(fileID,fmt,wardat);
                        if rDR==1 && cDR==1 || NuovoZero2DCrL == 0 % il file non è mai stato scritto, cerco la data precedente
                            NAN = 0;
                            ESCI = 1;
                            if datenum(dataini2DCrL)+1 < now
                                dataini2DCrL = datestr(datenum(dataini2DCrL)+1,'yyyy-mm-dd');
                                break
                            else
                                yes2DCrL = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini2DCrL = datestr(datenum(dataini2DCrL)-1,'yyyy-mm-dd');
                                break
                            else
                                if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                    Dati2DCrackLink(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                    Err2DCrackLink(1,jj) = 1;
                                    wardat = 'Data of 2D Crack Link nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                else
                                    NAN = 0;
                                    ESCI = 1;
                                    dataini2DCrL = datestr(datenum(dataini2DCrL)-1,'yyyy-mm-dd');
                                    break
                                end
                            end
                        end
                    end
                end
                if ESCI == 0
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',Dati2DCrackLink(:,jj)));
                        idxT = find(strcmp('---',Dati2DCrackLink(:,jj)));
                        idxT2 = find(strcmp('-',Dati2DCrackLink(:,jj)));
                        idxE2 = find(strcmp('Err2',Dati2DCrackLink(:,jj)));
                        idxE255 = find(strcmp('Err255',Dati2DCrackLink(:,jj)));
                        idxD = find(strcmp('DMUXe',Dati2DCrackLink(:,jj)));
                        idxE1F = find(strcmp('Err1File',Dati2DCrackLink(:,jj)));
                        idxN = find(strcmp('null',Dati2DCrackLink(:,jj)));
                        idxRX = find(strcmp('No RX',Dati2DCrackLink(:,jj)));
                        idxI = find(strcmp('id Error',Dati2DCrackLink(:,jj)));
                        idxM = find(strcmp('MUX id Error',Dati2DCrackLink(:,jj)));
                        idxC = find(strcmp('CH n. Error',Dati2DCrackLink(:,jj)));
                        idxU = find(strcmp('Unknown Error',Dati2DCrackLink(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',Dati2DCrackLink(:,jj)));
                        idxNS = find(strcmp('NotSupp',Dati2DCrackLink(:,jj)));
                        idxU2 = find(strcmp('Unknown',Dati2DCrackLink(:,jj)));
                        idxNA = find(strcmp('NotAv',Dati2DCrackLink(:,jj)));
                        idxND = find(strcmp('No Data',Dati2DCrackLink(:,jj)));
                        idxCE = find(strcmp('CH n. Er',Dati2DCrackLink(:,jj)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxT2);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxND);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini2DCrL = datestr(datenum(dataini2DCrL)-1,'yyyy-mm-dd');
                                break
                            else
                                Dati2DCrackLink(indice,jj) = Dati2DCrackLink(indice-1,jj);
                                Err2DCrackLink(indice,jj) = 1;
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(Dati2DCrackLink(nnn,jj)));
                            if check == 1
                                Dati2DCrackLink(nnn,jj) = Dati2DCrackLink(nnn-1,jj);
                                Err2DCrackLink(nnn,jj) = 1;
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of 2D Crack Link data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    Dati2DCrackLink = [DATini2DCrL str2double(Dati2DCrackLink)];
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if Dati2DCrackLink(a,1) == Dati2DCrackLink(a-1,1)
                            Dati2DCrackLink(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            end
        end
        
        if ESCI == 0
            if sum(CrL2D_NTC(:,1))>0
                % NTC
                % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
                %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
                [r,c] = size(DatiNTC2DCrackLink);
                Ncorr = 0;
                ErrNTC2DCrackLink = zeros(r,c);
                if NuovoZero2DCrL == 1
                    if isfile(RawDataFile) == 1
                        DatiRawNTC = csvread(RawDataFileNTC);
                        [rDR,cDR] = size(DatiRawNTC);
                        DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                        RawDateNTC = find(DatiRawNTC(:,1)<=datenum(dataini2DCrL));
                    else
                        rDR = 1;
                        cDR = 1;
                        RawDateNTC = [];
                        DatiRawNTC = [];
                    end
                else
                    rDR = 1;
                    cDR = 1;
                    RawDateNTC = [];
                    DatiRawNTC = [];
                end
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiNTC2DCrackLink(1,jj)));
                    idxT3 = find(strcmp('---',DatiNTC2DCrackLink(1,jj)));
                    idxT = find(strcmp('-',DatiNTC2DCrackLink(1,jj)));
                    idxE2 = find(strcmp('Err2',DatiNTC2DCrackLink(1,jj)));
                    idxE255 = find(strcmp('Err255',DatiNTC2DCrackLink(1,jj)));
                    idxD = find(strcmp('DMUXe',DatiNTC2DCrackLink(1,jj)));
                    idxE1F = find(strcmp('Err1File',DatiNTC2DCrackLink(1,jj)));
                    idxN = find(strcmp('null',DatiNTC2DCrackLink(1,jj)));
                    idxRX = find(strcmp('No RX',DatiNTC2DCrackLink(1,jj)));
                    idxI = find(strcmp('id Error',DatiNTC2DCrackLink(1,jj)));
                    idxM = find(strcmp('MUX id Error',DatiNTC2DCrackLink(1,jj)));
                    idxC = find(strcmp('CH n. Error',DatiNTC2DCrackLink(1,jj)));
                    idxU = find(strcmp('Unknown Error',DatiNTC2DCrackLink(1,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiNTC2DCrackLink(1,jj)));
                    idxNS = find(strcmp('NotSupp',DatiNTC2DCrackLink(1,jj)));
                    idxU2 = find(strcmp('Unknown',DatiNTC2DCrackLink(1,jj)));
                    idxNA = find(strcmp('NotAv',DatiNTC2DCrackLink(1,jj)));
                    idxND = find(strcmp('No Data',DatiNTC2DCrackLink(1,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiNTC2DCrackLink(1,jj)));
                    idx = union(idxE,idxT3);
                    idx = union(idx,idxND);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxT);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    [ri,~] = size(idx);
                    if ri == 1
                        wardat = 'Warning on the correction of 2D Crack Link nodes: there are not data at the beginning of the array!';
                        fprintf(fileID,fmt,wardat);
                        if rDR==1 && cDR==1 || NuovoZero2DCrL == 0 % il file non è mai stato scritto, cerco la data precedente
                            NAN = 0;
                            ESCI = 1;
                            if datenum(dataini2DCrL)+1 < now
                                dataini2DCrL = datestr(datenum(dataini2DCrL)+1,'yyyy-mm-dd');
                                break
                            else
                                yes2DCrL = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                dataini2DCrL = datestr(datenum(dataini2DCrL)-1,'yyyy-mm-dd');
                                break
                            else
                                Dati2DCrackLink(1,jj) = cellstr(num2str(DatiRaw(RawDateNTC(end),jj+1)));
                                Err2DCrackLink(1,jj) = 1;
                                wardat = 'Data of 2D Crack Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                    end
                end
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiNTC2DCrackLink(:,jj)));
                    idxT3 = find(strcmp('---',DatiNTC2DCrackLink(:,jj)));
                    idxT = find(strcmp('-',DatiNTC2DCrackLink(:,jj)));
                    idxE2 = find(strcmp('Err2',DatiNTC2DCrackLink(:,jj)));
                    idxE255 = find(strcmp('Err255',DatiNTC2DCrackLink(:,jj)));
                    idxD = find(strcmp('DMUXe',DatiNTC2DCrackLink(:,jj)));
                    idxE1F = find(strcmp('Err1File',DatiNTC2DCrackLink(:,jj)));
                    idxN = find(strcmp('null',DatiNTC2DCrackLink(:,jj)));
                    idxRX = find(strcmp('No RX',DatiNTC2DCrackLink(:,jj)));
                    idxI = find(strcmp('id Error',DatiNTC2DCrackLink(:,jj)));
                    idxM = find(strcmp('MUX id Error',DatiNTC2DCrackLink(:,jj)));
                    idxC = find(strcmp('CH n. Error',DatiNTC2DCrackLink(:,jj)));
                    idxU = find(strcmp('Unknown Error',DatiNTC2DCrackLink(:,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiNTC2DCrackLink(:,jj)));
                    idxNS = find(strcmp('NotSupp',DatiNTC2DCrackLink(:,jj)));
                    idxU2 = find(strcmp('Unknown',DatiNTC2DCrackLink(:,jj)));
                    idxNA = find(strcmp('NotAv',DatiNTC2DCrackLink(:,jj)));
                    idxND = find(strcmp('No Data',DatiNTC2DCrackLink(:,jj)));
                    idxCE = find(strcmp('CH n. Er',DatiNTC2DCrackLink(:,jj)));
                    idx = union(idxE,idxT3);
                    idx = union(idx,idxND);
                    idx = union(idx,idxCE);
                    idx = union(idx,idxT);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    [ri,~] = size(idx);
                    for kk=1:ri
                        indice=idx(kk);
                        if indice == 1
                            NAN = 0;
                            ESCI = 1;
                            dataini2DCrL = datestr(datenum(dataini2DCrL)-1,'yyyy-mm-dd');
                        else
                            DatiNTC2DCrackLink(indice,jj) = DatiNTC2DCrackLink(indice-1,jj);
                            ErrNTC2DCrackLink(indice,jj) = 1;
                            Ncorr = Ncorr+ri;
                            NAN = 1;
                        end
                    end
                    if ESCI == 1
                        break
                    end
                    % Elimino gli eventuali Not a Number
                    for nnn = 2:r
                        check = isnan(cell2mat(DatiNTC2DCrackLink(nnn,jj)));
                        if check == 1
                            DatiNTC2DCrackLink(nnn,jj) = DatiNTC2DCrackLink(nnn-1,jj);
                            ErrNTC2DCrackLink(nnn,jj) = 1;
                            Ncorr = Ncorr+ri;
                        end
                    end
                end
                corrdat = ['' num2str(Ncorr) ' corrections of NTC of 2D Crack Link data done!'];
                fprintf(fileID,fmt,corrdat);
                % Concateno informazioni generali e/o letture dai nodi.
                DatiNTC2DCrackLink = [DATini2DCrL str2double(DatiNTC2DCrackLink)];
                % Elimino eventuali date doppie
                a = 2;
                while a <= r
                    if DatiNTC2DCrackLink(a,1) == DatiNTC2DCrackLink(a-1,1)
                        DatiNTC2DCrackLink(a,:) = [];
                        r = r-1;
                    else
                        a = a+1;
                    end
                end
            else
                ErrNTC2DCrackLink = 0;
            end
        end
    end
    if NuovoZero2DCrL == 1 && yes2DCrL == 1
        [rDR,cDR] = size(DatiRaw);
        if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=Dati2DCrackLink(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(Dati2DCrackLink(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(Dati2DCrackLink(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);Dati2DCrackLink(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = Dati2DCrackLink;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
        
        if sum(CrL2D_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTC2DCrackLink(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTC2DCrackLink(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTC2DCrackLink(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTC2DCrackLink(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTC2DCrackLink;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = Dati2DCrackLink;
        if sum(CrL2D_NTC(:,1))>0
            DatiNTC = DatiNTC2DCrackLink;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(CrL2D_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
end

%%  Stress Meter
attiva_SM = zeros(1,rSM);
if  yesSM == 0 % Non ci sono Stress Meter
    DatiStressMeter = [];
    DatiNTCStressMeter = [];
    DatiStressMeter_Rif = [];
    DatiNTCStressMeter_Rif = [];
    ErrStressMeter = [];
    ChSM = [];
    SM_NTC = [];
    wardat = 'lettura function: there are not Stress Meter';
    fprintf(fileID,fmt,wardat);
else
    RawDataFile = ['' IDcentralina '-' DTcatena '-Stress-RawData.csv']; % Specifico per Stress Meter
    RawDataFileNTC = ['' IDcentralina '-' DTcatena '-NTCStress-RawData.csv'];
    ChSM = cell2mat(NodoStressMeter(:,4));
    SM_NTC = cell2mat(NodoStressMeter(:,5));
    NAN = 0;
    DIS = 0;
    while NAN == 0
        if datenum(datainiSM) == datenum(Data(1,1))
            tempoiniSM = char(Data(1,2));
        end
        NodeType = 'Stress Meter';
        NodeNumSM = num2str(cell2mat(NodoStressMeter(1,2))); % scarico i dati del primo nodo
        % Scarico dei dati relativi alla centralina con informazioni di Data e ora, livello batteria e temperatura
        % Step  1 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiSM ''' and Time >= ''' tempoiniSM ''' and UnitName = ''' IDcentralina...
            ''' and  ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
            NodeType ''' and NodeNum = ''' NodeNumSM ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATiniSM = curs.Data;
        Ntempo = DATiniSM;
        [r,c] = size(DATiniSM);
        saltoSM = 0;
        if r==1 && c==1
            textdatSM = 'Warning: error reading Stress Meter (control unit data)!';
            fprintf(fileID,fmt,textdatSM);
            saltoSM = 1;
        end
        % Step 2 (vedi commento più avanti)
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            datainiSM ''' and UnitName = ''' IDcentralina ...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType...
            ''' and NodeNum = ''' NodeNumSM ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DAT2SM = curs.Data;
        Ndate = DAT2SM;
        if saltoSM == 1
            DATiniSM = DAT2SM;
        end
        Check = size(DAT2SM);
        if Check(1,2) > 1 % ho superato il primo giorno
            if saltoSM == 0
                % Concateno le informazioni relative a Step 1 e 2 (se necessario...)
                DATiniSM = [DATiniSM; DAT2SM];
            end
        end
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATiniSM);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATiniSM(:,1)) repmat(' ', [rD,1]) cell2mat(DATiniSM(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DATiniSM = T;
        else
            DATiniSM = [];
        end
        
        % Scarico dati relativi ai singoli nodi
        [rd,~] = size(Ndate); % numero di dati successivi al primo giorno di calcolo
        [rt,~] = size(Ntempo); % numero di dati del primo giorno di calcolo
        % creo la matrice per i dati Extensometer Link
        if saltoSM == 0
            if Check(1,2) > 1
                DatiStressMeter = cell(rd+rt,rSM);
                DatiNTCStressMeter = cell(rd+rt,rSM);
            else
                DatiStressMeter = cell(rt,rSM);
                DatiNTCStressMeter = cell(rt,rSM);
            end
        else
            DatiStressMeter = cell(rd,rSM);
            DatiNTCStressMeter = cell(rd,rSM);
        end
        DatiStressMeter_Rif = cell(150,rSM);
        DatiNTCStressMeter_Rif = cell(150,rSM);
        NStress = cell2mat(NodoStressMeter(:,2));
        col = 1;
        ii = 1;
        w = 0;
        while ii <= rSM
            % Step 1: Acquisisco i dati registrati successivamente all'ultimo scarico,
            % ma limitandomi a quelli registrati nel corso di quella singola giornata;
            nN = NStress(ii,1);
            nNodo = num2str(nN);
            % Dato
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datainiSM ''' and Time >= ''' tempoiniSM ''' and  ToolNameID = ''' ...
                DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoSM = curs.Data;
            % Dato
            comando = ['select Val0 from RawDataView where Date > ''' ...
                datainiSM ''' and UnitName = ''' IDcentralina ...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                NodeType ''' and NodeNum = '  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoSMd = curs.Data;
            % Riferimento
            comando = ['select Val0 from RawDataView where Date = ''' ...
                datestr(Date_Rif_SM,'yyyy-mm-dd') ''' and Time >= ''' datestr(Date_Rif_SM,'HH:MM:SS')...
                ''' and  ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  nNodo ''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoSM_Rif = curs.Data;
            if ChSM(ii,1) == 1
                % T
                DATnodoSM_NTC = 0;
                DATnodoSMd_NTC = 0;
            elseif ChSM(ii,1) == 2
                if SM_NTC(ii,1)==1
                    comando = ['select Val1 from RawDataView where Date = ''' ...
                        datainiSM ''' and Time >= ''' tempoiniSM ''' and  ToolNameID = ''' ...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoSM_NTC = curs.Data;
                    % Dato
                    comando = ['select Val1 from RawDataView where Date > ''' ...
                        datainiSM ''' and UnitName = ''' IDcentralina ...
                        ''' and ToolNameID = ''' DTcatena ''' and NodeType = '''...
                        NodeType ''' and NodeNum = '  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoSMd_NTC = curs.Data;
                    % Riferimento
                    comando = ['select Val1 from RawDataView where Date = ''' ...
                        datestr(Date_Rif_SM,'yyyy-mm-dd') ''' and Time >= ''' datestr(Date_Rif_SM,'HH:MM:SS')...
                        ''' and  ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum ='  nNodo ''];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoNTCSM_Rif = curs.Data;
                else
                    DATnodoSM_NTC = 0;
                    DATnodoSMd_NTC = 0;
                end
            end
                
            [rn,~] = size(DATnodoSMd); % controllo date
            if rn < rd % normalmente sono uguali
                for i = rn+1:rd
                    DATnodoSMd(i,:) = DATnodoSMd(rn,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['Node Stress Meter ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            [rnT,~] = size(DATnodoSMd_NTC); % controllo date
            if rnT < rd % normalmente sono uguali
                for i = rnT+1:rd
                    DATnodoSMd_NTC(i,:) = DATnodoSMd_NTC(rnT,:); % se mancano dati, copio gli ultimi
                end
                wardat = ['NTC of Node Stress Meter ' num2str(ii) ' does NOT work!'];
                fprintf(fileID,fmt,wardat);
                w = 1;
            end
            
            % DATnodo è la matrice che raccoglie i dati dell' ii-esimo nodo,
            % DATnodi è la matrice che concatena i dati relativi a ciascun nodo.
            if saltoSM == 0
                if ChSM(ii,1) >= 2
                    if SM_NTC(ii,1)==1
                        DatiStressMeter(1:rt,col) = DATnodoSM(:,:); %#ok<*AGROW>
                        DatiNTCStressMeter(1:rt,col) = DATnodoSM_NTC(:,:); %#ok<*AGROW>
                        if Check(1,2) > 1
                            [rrr,~] = size(DATnodoSMd);
                            if rrr ~= rd  
                                DatiStressMeter(rt+rd-rrr+1:rt+rd,col) = DATnodoSMd;
                                DatiNTCStressMeter(rt+rd-rrr+1:rt+rd,col) = DATnodoSMd_NTC; %#ok<*AGROW>
                            else
                                DatiStressMeter(rt+1:rt+rd,col) = DATnodoSMd(:,:);
                                DatiNTCStressMeter(rt+1:rt+rd,col) = DATnodoSMd_NTC(:,:); %#ok<*AGROW>
                            end
                        end
                    else
                        DatiStressMeter(1:rt,col) = DATnodoSM(:,:); %#ok<*AGROW>
                        if Check(1,2) > 1
                            DatiStressMeter(rt+1:rt+rd,col) = DATnodoSMd(:,:);
                        end
                    end
                elseif ChSM(ii,1) == 1
                    DatiStressMeter(1:rt,col) = DATnodoSM(:,:); %#ok<*AGROW>
                    if Check(1,2) > 1
                        DatiStressMeter(rt+1:rt+rd,col) = DATnodoSMd(:,:);
                    end
                end                        
            else
                if ChSM(ii,1) >= 2
                    if SM_NTC(ii,1)==1
                        DatiStressMeter(1:rd,col) = DATnodoSM(:,:); %#ok<*AGROW>
                        DatiNTCStressMeter(1:rd,col) = DATnodoSM_NTC(:,:); %#ok<*AGROW>
                    else
                        DatiStressMeter(1:rd,col) = DATnodoSM(:,:); %#ok<*AGROW>
                    end
                elseif ChSM(ii,1) == 1
                    DatiStressMeter(1:rd,col) = DATnodoSM(:,:); %#ok<*AGROW>
                end
            end
            
             % Riferimento
            [dim,~] = size(DATnodoSM_Rif); % controllo date
            DatiStressMeter_Rif(1:dim,col) = DATnodoSM_Rif;
            if col == 1
               DatiStressMeter_Rif(dim+1:end,:) = [];
            end
            [dimT,~] = size(DATnodoNTCSM_Rif); % controllo date
            DatiNTCStressMeter_Rif(1:dimT,col) = DATnodoNTCSM_Rif;
            if col == 1
               DatiNTCStressMeter_Rif(dim+1:end,:) = [];
            end
            
            okdatSM = ['Data of node number ' num2str(ii) ' of ' ...
                num2str(rSM) ' Stress Meter nodes downloaded correctly'];
            fprintf(fileID,fmt,okdatSM);
            ii = ii+1;
            col = col+1;
        end
        if w == 0
            wardat = 'There are not warning for Stress Meter!';
            fprintf(fileID,fmt,wardat);
        end

        % Controllo gli eventuali errori ('Err1' o '---') e li rimuovo,
        %  rimpiazzandoli con l'ultimo dato disponibile di quel nodo
        
        % Dati
        [r,c] = size(DatiStressMeter);
        ErrStressMeter = zeros(r,c);
        Ncorr = 0;
        ESCI = 0;
        Rilancia = 0;
        NAN = 1;
        if NuovoZeroSM == 1
            if isfile(RawDataFile) == 1
                DatiRaw = csvread(RawDataFile);
                [rDR,cDR] = size(DatiRaw);
                DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                RawDate = find(DatiRaw(:,1)<=datenum(datainiSM));
            else
                rDR = 1;
                cDR = 1;
                RawDate = [];
                DatiRaw = [];
            end
        else
            rDR = 1;
            cDR = 1;
            RawDate = [];
            DatiRaw = [];
        end
        [dim,numS] = size(DatiStressMeter_Rif);
        for SM = 1:numS
            RIF = 0;
            row = 1;
            rompi = 0;
            while RIF == 0
                if attiva_SM(1,SM) == 0
                    if row <= dim
                        % Controllo la data di riferimento
                        idxE = find(strcmp('Err1',DatiStressMeter_Rif(row,SM)));
                        idxT = find(strcmp('---',DatiStressMeter_Rif(row,SM)));
                        idxE2 = find(strcmp('Err2',DatiStressMeter_Rif(row,SM)));
                        idxE255 = find(strcmp('Err255',DatiStressMeter_Rif(row,SM)));
                        idxD = find(strcmp('DMUXe',DatiStressMeter_Rif(row,SM)));
                        idxE1F = find(strcmp('Err1File',DatiStressMeter_Rif(row,SM)));
                        idxN = find(strcmp('null',DatiStressMeter_Rif(row,SM)));
                        idxRX = find(strcmp('No RX',DatiStressMeter_Rif(row,SM)));
                        idxI = find(strcmp('id Error',DatiStressMeter_Rif(row,SM)));
                        idxM = find(strcmp('MUX id Error',DatiStressMeter_Rif(row,SM)));
                        idxC = find(strcmp('CH n. Error',DatiStressMeter_Rif(row,SM)));
                        idxU = find(strcmp('Unknown Error',DatiStressMeter_Rif(row,SM)));
                        idxMC = find(strcmp('MsgCorrup',DatiStressMeter_Rif(row,SM)));
                        idxNS = find(strcmp('NotSupp',DatiStressMeter_Rif(row,SM)));
                        idxU2 = find(strcmp('Unknown',DatiStressMeter_Rif(row,SM)));
                        idxNA = find(strcmp('NotAv',DatiStressMeter_Rif(row,SM)));
                        idxDIS = find(strcmp('Dis.',DatiStressMeter_Rif(row,SM)));
                        idxND = find(strcmp('No Data',DatiStressMeter_Rif(row,SM)));
                        idxPP = find(strcmp('[]',DatiStressMeter_Rif(row,SM)));
                        idxCE = find(strcmp('CH n. Er',DatiStressMeter_Rif(row,SM)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxCE);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxDIS);
                        idx = union(idx,idxND);
                        idx = union(idx,idxPP);
                        [~,ri] = size(idx);
                        if isempty(idx) == 0 || isempty(cell2mat(DatiStressMeter_Rif(row,SM))) == 1
                            if ri >= 1 % Se almeno uno è in errore, allora guardo la riga dopo
                                row = row+1;
                            else
                                RIF = 1;
                                if attiva_SM(1,SM) == 0
                                    DatiStress_Rif(1,SM) = DatiStressMeter_Rif(row,SM);
                                    DatiNTCStress_Rif(1,SM) = str2double(DatiNTCStressMeter_Rif(row,SM));
                                end
                                attiva_SM(1,SM) = 1;
                            end
                        else
                            RIF = 1;
                            if attiva_SM(1,SM) == 0
                                DatiStress_Rif(1,SM) = DatiStressMeter_Rif(row,SM);
                                DatiNTCStress_Rif(1,SM) = str2double(DatiNTCStressMeter_Rif(row,SM));
                            end
                            attiva_SM(1,SM) = 1;
                        end
                    else
                        Date_Rif_SM = floor(Date_Rif_SM)+1;
                        if Date_Rif_SM > now
                            Rilancia = 0;
                            RIF = 1;
                            DatiStress_Rif(1,SM) = num2cell(0);
                            DatiNTCStress_Rif(1,SM) = 0;
                            attiva_SM(1,SM) = 0;
                        else
                            RIF = 1;
                            NAN = 0;
                            Rilancia = 1;
                        end
                        rompi = 1;
                    end
                else
                    break
                end
            end
            if rompi == 1
                break
            end
        end
        
        if Rilancia == 0
            for jj=1:c
                % Se il nodo è Dis., devo aumentare di uno la data (il nodo non
                % è ancora stato installato!)
                if strcmp('Dis.',DatiStressMeter(1,jj)) == 1
                    for dis = 2:r
                        if strcmp('Dis.',DatiStressMeter(dis,jj)) == 0
                            DatiStressMeter = DatiStressMeter(dis:end,:);
                            DATiniSM = DATiniSM(dis:end,:);
                            [r,c] = size(DatiStressMeter);
                            break
                        elseif dis == r
                            NAN = 0;
                            DIS = 1;
                            if datenum(datainiSM)+1 < now
                                datainiSM = datestr(datenum(datainiSM)+1,'yyyy-mm-dd');
                                break
                            else
                                yesSM = 0;
                            end
                        end
                    end
                    
                end
            end
            if DIS ~= 1 && yesSM == 1
                % --- Codice per il primo dato ---
                for jj=1:c
                    % Trovo l'indice della cella contenenti una determinata stringa.
                    idxE = find(strcmp('Err1',DatiStressMeter(1,jj)));
                    idxT = find(strcmp('---',DatiStressMeter(1,jj)));
                    idxT2 = find(strcmp('-',DatiStressMeter(1,jj)));
                    idxE2 = find(strcmp('Err2',DatiStressMeter(1,jj)));
                    idxE255 = find(strcmp('Err255',DatiStressMeter(1,jj)));
                    idxD = find(strcmp('DMUXe',DatiStressMeter(1,jj)));
                    idxE1F = find(strcmp('Err1File',DatiStressMeter(1,jj)));
                    idxN = find(strcmp('null',DatiStressMeter(1,jj)));
                    idxRX = find(strcmp('No RX',DatiStressMeter(1,jj)));
                    idxI = find(strcmp('id Error',DatiStressMeter(1,jj)));
                    idxM = find(strcmp('MUX id Error',DatiStressMeter(1,jj)));
                    idxC = find(strcmp('CH n. Error',DatiStressMeter(1,jj)));
                    idxU = find(strcmp('Unknown Error',DatiStressMeter(1,jj)));
                    idxMC = find(strcmp('MsgCorrup',DatiStressMeter(1,jj)));
                    idxNS = find(strcmp('NotSupp',DatiStressMeter(1,jj)));
                    idxU2 = find(strcmp('Unknown',DatiStressMeter(1,jj)));
                    idxNA = find(strcmp('NotAv',DatiStressMeter(1,jj)));
                    idxND = find(strcmp('No Data',DatiStressMeter(1,jj)));
                    idx = union(idxE,idxT);
                    idx = union(idx,idxND);
                    idx = union(idx,idxT2);
                    idx = union(idx,idxE2);
                    idx = union(idx,idxE255);
                    idx = union(idx,idxD);
                    idx = union(idx,idxE1F);
                    idx = union(idx,idxN);
                    idx = union(idx,idxRX);
                    idx = union(idx,idxI);
                    idx = union(idx,idxM);
                    idx = union(idx,idxC);
                    idx = union(idx,idxU);
                    idx = union(idx,idxMC);
                    idx = union(idx,idxNS);
                    idx = union(idx,idxU2);
                    idx = union(idx,idxNA);
                    idx = union(idx,idxND);
                    [ri,~] = size(idx);
                    if ri == 1
                        wardat = 'Warning on the correction of Stress Meter nodes: there are not data at the beginning of the array!';
                        fprintf(fileID,fmt,wardat);
                        if rDR==1 && cDR==1 || NuovoZeroSM == 0 % il file non è mai stato scritto, cerco la data precedente
                            NAN = 0;
                            ESCI = 1;
                            if datenum(datainiSM)+1 < now
                                datainiSM = datestr(datenum(datainiSM)+1,'yyyy-mm-dd');
                                break
                            else
                                yesSM = 0;
                            end
                        else
                            if isempty(RawDate) == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiSM = datestr(datenum(datainiSM)-1,'yyyy-mm-dd');
                                break
                            else
                                if isnan(DatiRaw(RawDate(end),jj+1)) == 0
                                    DatiStressMeter(1,jj) = cellstr(num2str(DatiRaw(RawDate(end),jj+1)));
                                    ErrStressMeter(1,jj) = 1;
                                    wardat = 'Data of Stress Meter nodes corrected using Raw Data of reference Csv file.';
                                    fprintf(fileID,fmt,wardat);
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                else
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiSM = datestr(datenum(datainiSM)-1,'yyyy-mm-dd');
                                    break
                                end
                            end
                        end
                    end
                end
                if ESCI == 0
                    for jj=1:c
                        % Trovo l'indice della cella contenenti una determinata stringa.
                        idxE = find(strcmp('Err1',DatiStressMeter(:,jj)));
                        idxT = find(strcmp('---',DatiStressMeter(:,jj)));
                        idxT2 = find(strcmp('-',DatiStressMeter(:,jj)));
                        idxE2 = find(strcmp('Err2',DatiStressMeter(:,jj)));
                        idxE255 = find(strcmp('Err255',DatiStressMeter(:,jj)));
                        idxD = find(strcmp('DMUXe',DatiStressMeter(:,jj)));
                        idxE1F = find(strcmp('Err1File',DatiStressMeter(:,jj)));
                        idxN = find(strcmp('null',DatiStressMeter(:,jj)));
                        idxRX = find(strcmp('No RX',DatiStressMeter(:,jj)));
                        idxI = find(strcmp('id Error',DatiStressMeter(:,jj)));
                        idxM = find(strcmp('MUX id Error',DatiStressMeter(:,jj)));
                        idxC = find(strcmp('CH n. Error',DatiStressMeter(:,jj)));
                        idxU = find(strcmp('Unknown Error',DatiStressMeter(:,jj)));
                        idxMC = find(strcmp('MsgCorrup',DatiStressMeter(:,jj)));
                        idxNS = find(strcmp('NotSupp',DatiStressMeter(:,jj)));
                        idxU2 = find(strcmp('Unknown',DatiStressMeter(:,jj)));
                        idxNA = find(strcmp('NotAv',DatiStressMeter(:,jj)));
                        idxND = find(strcmp('No Data',DatiStressMeter(:,jj)));
                        idx = union(idxE,idxT);
                        idx = union(idx,idxND);
                        idx = union(idx,idxT2);
                        idx = union(idx,idxE2);
                        idx = union(idx,idxE255);
                        idx = union(idx,idxD);
                        idx = union(idx,idxE1F);
                        idx = union(idx,idxN);
                        idx = union(idx,idxRX);
                        idx = union(idx,idxI);
                        idx = union(idx,idxM);
                        idx = union(idx,idxC);
                        idx = union(idx,idxU);
                        idx = union(idx,idxMC);
                        idx = union(idx,idxNS);
                        idx = union(idx,idxU2);
                        idx = union(idx,idxNA);
                        idx = union(idx,idxND);
                        [ri,~] = size(idx);
                        for kk=1:ri
                            indice=idx(kk);
                            if indice == 1
                                NAN = 0;
                                ESCI = 1;
                                datainiSM = datestr(datenum(datainiSM)-1,'yyyy-mm-dd');
                                break
                            else
                                DatiStressMeter(indice,jj) = DatiStressMeter(indice-1,jj);
                                ErrStressMeter(indice,jj) = 1;
                                Ncorr = Ncorr+ri;
                                NAN = 1;
                            end
                        end
                        if ESCI == 1
                            break
                        end
                        % Elimino gli eventuali Not a Number
                        for nnn = 2:r
                            check = isnan(cell2mat(DatiStressMeter(nnn,jj)));
                            if check == 1
                                DatiStressMeter(nnn,jj) = DatiStressMeter(nnn-1,jj);
                                ErrStressMeter(nnn,jj) = 1;
                                Ncorr = Ncorr+ri;
                            end
                        end
                    end
                    corrdat = ['' num2str(Ncorr) ' corrections of Stress Meter data done!'];
                    fprintf(fileID,fmt,corrdat);
                    % Concateno informazioni generali e/o letture dai nodi.
                    DatiStressMeter = [DATiniSM str2double(DatiStressMeter)];
                    DatiStress_Rif = str2double(DatiStress_Rif);
                    % Elimino eventuali date doppie
                    a = 2;
                    while a <= r
                        if DatiStressMeter(a,1) == DatiStressMeter(a-1,1)
                            DatiStressMeter(a,:) = [];
                            r = r-1;
                        else
                            a = a+1;
                        end
                    end
                end
            end
        
            if ESCI == 0
                if sum(SM_NTC(:,1))>0
                    if DIS ~= 1 && yesSM == 1
                        % NTC
                        [r,c] = size(DatiNTCStressMeter);
                        Ncorr = 0;
                        if NuovoZeroSM == 1
                            if isfile(RawDataFile) == 1
                                DatiRawNTC = csvread(RawDataFileNTC);
                                [rDR,cDR] = size(DatiRawNTC);
                                DatiRawNTC(:,1) = DatiRawNTC(:,1) + 730000;
                                RawDateNTC = find(DatiRawNTC(:,1)<=datenum(datainiSM));
                            else
                                rDR = 1;
                                cDR = 1;
                                DatiRawNTC = [];
                                RawDateNTC = [];
                            end
                        else
                            rDR = 1;
                            cDR = 1;
                            RawDateNTC = [];
                            DatiRawNTC = [];
                        end
                        for jj=1:c
                            % Trovo l'indice della cella contenenti una determinata stringa.
                            idxE = find(strcmp('Err1',DatiNTCStressMeter(1,jj)));
                            idxND = find(strcmp('No Data',DatiNTCStressMeter(1,jj)));
                            idxT3 = find(strcmp('---',DatiNTCStressMeter(1,jj)));
                            idxT = find(strcmp('-',DatiNTCStressMeter(1,jj)));
                            idxE2 = find(strcmp('Err2',DatiNTCStressMeter(1,jj)));
                            idxE255 = find(strcmp('Err255',DatiNTCStressMeter(1,jj)));
                            idxD = find(strcmp('DMUXe',DatiNTCStressMeter(1,jj)));
                            idxE1F = find(strcmp('Err1File',DatiNTCStressMeter(1,jj)));
                            idxN = find(strcmp('null',DatiNTCStressMeter(1,jj)));
                            idxRX = find(strcmp('No RX',DatiNTCStressMeter(1,jj)));
                            idxI = find(strcmp('id Error',DatiNTCStressMeter(1,jj)));
                            idxM = find(strcmp('MUX id Error',DatiNTCStressMeter(1,jj)));
                            idxC = find(strcmp('CH n. Error',DatiNTCStressMeter(1,jj)));
                            idxU = find(strcmp('Unknown Error',DatiNTCStressMeter(1,jj)));
                            idxMC = find(strcmp('MsgCorrup',DatiNTCStressMeter(1,jj)));
                            idxNS = find(strcmp('NotSupp',DatiNTCStressMeter(1,jj)));
                            idxU2 = find(strcmp('Unknown',DatiNTCStressMeter(1,jj)));
                            idxNA = find(strcmp('NotAv',DatiNTCStressMeter(1,jj)));
                            idx = union(idxE,idxT3);
                            idx = union(idx,idxT);
                            idx = union(idx,idxND);
                            idx = union(idx,idxE2);
                            idx = union(idx,idxE255);
                            idx = union(idx,idxD);
                            idx = union(idx,idxE1F);
                            idx = union(idx,idxN);
                            idx = union(idx,idxRX);
                            idx = union(idx,idxI);
                            idx = union(idx,idxM);
                            idx = union(idx,idxC);
                            idx = union(idx,idxU);
                            idx = union(idx,idxMC);
                            idx = union(idx,idxNS);
                            idx = union(idx,idxU2);
                            idx = union(idx,idxNA);
                            [ri,~] = size(idx);
                            if ri == 1
                                wardat = 'Warning on the correction of NTC of Stress Meter nodes: there are not data at the beginning of the array!';
                                fprintf(fileID,fmt,wardat);
                                if rDR==1 && cDR==1 || NuovoZeroSM == 0 % il file non è mai stato scritto, cerco la data precedente
                                    NAN = 0;
                                    ESCI = 1;
                                    if datenum(datainiSM)+1 < now
                                        datainiSM = datestr(datenum(datainiSM)+1,'yyyy-mm-dd');
                                        break
                                    else
                                        yesSM = 0;
                                    end
                                else
                                    if isempty(RawDateNTC) == 1
                                        NAN = 0;
                                        ESCI = 1;
                                        datainiSM = datestr(datenum(datainiSM)-1,'yyyy-mm-dd');
                                        break
                                    else
                                        DatiNTCStressMeter(1,jj) = cellstr(num2str(DatiRawNTC(RawDateNTC(end),jj+1)));
                                        if ErrStressMeter(indice,jj) == 0
                                            ErrStressMeter(indice,jj) = 0.5;
                                        else
                                            ErrStressMeter(indice,jj) = 1;
                                        end
                                        wardat = 'Data of NTC of Stress Meter nodes corrected using Raw Data of reference Csv file.';
                                        fprintf(fileID,fmt,wardat);
                                        Ncorr = Ncorr+ri;
                                        NAN = 1;
                                    end
                                end
                            end
                        end
                        for jj=1:c
                            % Trovo l'indice della cella contenenti una determinata stringa.
                            idxE = find(strcmp('Err1',DatiNTCStressMeter(:,jj)));
                            idxND = find(strcmp('No Data',DatiNTCStressMeter(:,jj)));
                            idxT3 = find(strcmp('---',DatiNTCStressMeter(:,jj)));
                            idxT = find(strcmp('-',DatiNTCStressMeter(:,jj)));
                            idxE2 = find(strcmp('Err2',DatiNTCStressMeter(:,jj)));
                            idxE255 = find(strcmp('Err255',DatiNTCStressMeter(:,jj)));
                            idxD = find(strcmp('DMUXe',DatiNTCStressMeter(:,jj)));
                            idxE1F = find(strcmp('Err1File',DatiNTCStressMeter(:,jj)));
                            idxN = find(strcmp('null',DatiNTCStressMeter(:,jj)));
                            idxRX = find(strcmp('No RX',DatiNTCStressMeter(:,jj)));
                            idxI = find(strcmp('id Error',DatiNTCStressMeter(:,jj)));
                            idxM = find(strcmp('MUX id Error',DatiNTCStressMeter(:,jj)));
                            idxC = find(strcmp('CH n. Error',DatiNTCStressMeter(:,jj)));
                            idxU = find(strcmp('Unknown Error',DatiNTCStressMeter(:,jj)));
                            idxMC = find(strcmp('MsgCorrup',DatiNTCStressMeter(:,jj)));
                            idxNS = find(strcmp('NotSupp',DatiNTCStressMeter(:,jj)));
                            idxU2 = find(strcmp('Unknown',DatiNTCStressMeter(:,jj)));
                            idxNA = find(strcmp('NotAv',DatiNTCStressMeter(:,jj)));
                            idx = union(idxE,idxT3);
                            idx = union(idx,idxND);
                            idx = union(idx,idxT);
                            idx = union(idx,idxE2);
                            idx = union(idx,idxE255);
                            idx = union(idx,idxD);
                            idx = union(idx,idxE1F);
                            idx = union(idx,idxN);
                            idx = union(idx,idxRX);
                            idx = union(idx,idxI);
                            idx = union(idx,idxM);
                            idx = union(idx,idxC);
                            idx = union(idx,idxU);
                            idx = union(idx,idxMC);
                            idx = union(idx,idxNS);
                            idx = union(idx,idxU2);
                            idx = union(idx,idxNA);
                            [ri,~] = size(idx);
                            for kk=1:ri
                                indice=idx(kk);
                                if indice == 1
                                    NAN = 0;
                                    ESCI = 1;
                                    datainiSM = datestr(datenum(datainiSM)-1,'yyyy-mm-dd');
                                    break
                                else
                                    DatiNTCStressMeter(indice,jj) = DatiNTCStressMeter(indice-1,jj);
                                    if ErrStressMeter(indice,jj) == 0
                                        ErrStressMeter(indice,jj) = 0.5;
                                    else
                                        ErrStressMeter(indice,jj) = 1;
                                    end
                                    Ncorr = Ncorr+ri;
                                    NAN = 1;
                                end
                            end
                            if ESCI == 1
                                break
                            end
                            % Elimino gli eventuali Not a Number
                            for nnn = 2:r
                                check = isnan(cell2mat(DatiNTCStressMeter(nnn,jj)));
                                if check == 1
                                    DatiNTCStressMeter(nnn,jj) = DatiNTCStressMeter(nnn-1,jj);
                                    if ErrStressMeter(indice,jj) == 0
                                        ErrStressMeter(indice,jj) = 0.5;
                                    else
                                        ErrStressMeter(indice,jj) = 1;
                                    end
                                    Ncorr = Ncorr+ri;
                                end
                            end
                        end
                        corrdat = ['' num2str(Ncorr) ' corrections of NTC Stress Meter data done!'];
                        fprintf(fileID,fmt,corrdat);
                        % Concateno informazioni generali e/o letture dai nodi.
                        DatiNTCStressMeter = [DATiniSM str2double(DatiNTCStressMeter)];
                        % Elimino eventuali date doppie
                        a = 2;
                        while a <= r
                            if DatiNTCStressMeter(a,1) == DatiNTCStressMeter(a-1,1)
                                DatiNTCStressMeter(a,:) = [];
                                r = r-1;
                            else
                                a = a+1;
                            end
                        end
                    end
                end
            end
        end
    end
    if NuovoZeroSM == 1 && yesSM == 1
        [rDR,cDR] = size(DatiRaw);
        if rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
            RawDate1 = find(DatiRaw(:,1)<=DatiStressMeter(1,1));
            if isempty(RawDate1) == 1
                RawDate2 = 1;
            elseif RawDate1(end) == rDR
                RawDate2 = find(DatiStressMeter(:,1)>DatiRaw(end,1));
            else
                RawDate2 = find(DatiStressMeter(:,1)>DatiRaw(RawDate1(end)+1,1));
            end
        else
            RawDate1 = [];
            RawDate2 = 1;
        end
        if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
            Dati = [DatiRaw(1:RawDate1(end),:);DatiStressMeter(RawDate2(1):end,:)];
        elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
            Dati = DatiStressMeter;
        else
            Dati = DatiRaw;
        end
        % Elimino appoggio più vecchio di un mese
        RawDate3 = find(Dati(:,1)<now-30);
        if isempty(RawDate3) == 0
            [rDati,~] = size(Dati);
            if RawDate3(end) == rDati
            else
                Dati = Dati(RawDate3(end)+1:end,:);
            end
        end
        if isfile(RawDataFile) == 1
            delete(RawDataFile);
        end
        if sum(SM_NTC(:,1))>0
            [rDR,cDR] = size(DatiRawNTC);
            if  rDR~=1 && cDR~=1 && isempty(DatiRawNTC) == 0
                RawDate1 = find(DatiRawNTC(:,1)<=DatiNTCStressMeter(1,1));
                if isempty(RawDate1) == 1
                    RawDate2 = 1;
                elseif RawDate1(end) == rDR
                    RawDate2 = find(DatiNTCStressMeter(:,1)>DatiRawNTC(end,1));
                else
                    RawDate2 = find(DatiNTCStressMeter(:,1)>DatiRawNTC(RawDate1(end)+1,1));
                end
            else
                RawDate1 = [];
                RawDate2 = 1;
            end
            if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
                DatiNTC = [DatiRawNTC(1:RawDate1(end),:);DatiNTCStressMeter(RawDate2(1):end,:)];
            elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
                DatiNTC = DatiNTCStressMeter;
            else
                DatiNTC = DatiRawNTC;
            end
            % Elimino appoggio più vecchio di un mese
            RawDate3 = find(DatiNTC(:,1)<now-30);
            if isempty(RawDate3) == 0
                [rDati,~] = size(DatiNTC);
                if RawDate3(end) == rDati
                else
                    DatiNTC = DatiNTC(RawDate3(end)+1:end,:);
                end
            end
            if isfile(RawDataFileNTC) == 1
                delete(RawDataFileNTC);
            end
        end
    else
        Dati = DatiStressMeter;
        if sum(SM_NTC(:,1))>0
            DatiNTC = DatiNTCStressMeter;
        end
    end
    Dati(:,1) = Dati(:,1) - 730000;
    csvwrite(RawDataFile,Dati);
    if sum(SM_NTC(:,1))>0
        DatiNTC(:,1) = DatiNTC(:,1) - 730000;
        csvwrite(RawDataFileNTC,DatiNTC);
    end
    DatiStressMeter_Rif = DatiStress_Rif;
    DatiNTCStressMeter_Rif = DatiNTCStress_Rif;
end

corrdat = 'lettura function executed';
fprintf(fileID,fmt,corrdat);
fclose(fileID);

end