function [DatiElabTiltLink,NuovoZeroTL,datainiTL,tempoiniTL,...
    DatiElabTiltLinkHD,NuovoZeroHD,datainiHD,tempoiniHD,...
    DatiElabTiltLinkHDVR,NuovoZeroHDVR,datainiHDVR,tempoiniHDVR,...
    DatiElabTiltLinkHR,NuovoZeroTLHR,datainiTLHR,tempoiniTLHR,...
    DatiElabTiltLinkH,NuovoZeroTLH,datainiTLH,tempoiniTLH,...
    DatiElabTiltLinkHRH,NuovoZeroTLHRH,datainiTLHRH,tempoiniTLHRH,...
    DatiElabInPlaceLink,NuovoZeroIPL,datainiIPL,tempoiniIPL,...
    DatiElabInPlaceLinkHR,NuovoZeroIPLHR,datainiIPLHR,tempoiniIPLHR,...
    NuovoZeroPL,datainiPL,tempoiniPL,NuovoZeroBL,datainiBL,tempoiniBL,Barometro,...
    NuovoZeroThL,datainiThL,tempoiniThL,NuovoZeroLL,datainiLL,tempoiniLL,...
    NuovoZeroKL,datainiKL,tempoiniKL,NuovoZeroKLHR,datainiKLHR,tempoiniKLHR,...
    DatiElabRainLink,NuovoZeroRL,datainiRL,tempoiniRL,datainiSPP,tempoiniSPP,NuovoZeroSPP,...
    DatiElabSnowLink,NuovoZeroSL,datainiSL,tempoiniSL,...
    NuovoZeroPT100,datainiPT100,tempoiniPT100,NuovoZeroWL,datainiWL,tempoiniWL,...
    NuovoZeroPE,datainiPE,tempoiniPE,margine,marginePiezo] = LastElab(...
    conn,date,time,IDcentralina,DTcatena,Wdevst,Wdevst_HR,NdatiMedia,Ndatidespike,NdatiMediaP,...
    NodoTiltLink,yesTL,rTL,NodoTiltLinkHD,yesHD,rHD,NodoTiltLinkHDVR,yesHDVR,rHDVR,...
    NodoTiltLinkHR,yesTLHR,rTLHR,NodoTiltLinkH,yesTLH,rTLH,NodoTiltLinkHRH,yesTLHRH,rTLHRH,...
    NodoPiezoLink,yesPL,NodoBaroLink,yesBL,Barometri,...
    NodoLoadLink,yesLL,NodoThermLink,yesThL,NodoRainLink,yesRL,rRL,...
    NodoSnowLink,yesSL,rSL,NodoSPPLink,yesSPP,NodoKlinoLink,yesKL,rKL,...
    NodoKlinoLinkHR,yesKLHR,rKLHR,NodoPT100Link,yesPT100,NodoInPlaceLink,yesIPL,rIPL,...
    NodoInPlaceLinkHR,yesIPLHR,rIPLHR,NodoWeirLink,yesWL,NodoPendulum,yesPE,FileName)

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

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

if isempty(Wdevst)==1
    Wdevst = 1;
end

%% Tilt Link V
if  yesTL == 1 % Importo ultimo dato elaborato dei Tilt Link V
    NodeNumTL = num2str(cell2mat(NodoTiltLink(1,2))); % scarico i dati del primo nodo Tilt Link V
    NodeType = 'Tilt Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo

    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum = '  NodeNumTL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiTL = datestr(data1,'yyyy-mm-dd');
        tempoiniTL = datestr(data1,'HH:MM:SS');
        NuovoZeroTL = 0;
        DatiElabTiltLink = [];
        margine = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiTL = datestr(data2,'yyyy-mm-dd');
            tempoiniTL = datestr(data2,'HH:MM:SS');
            NuovoZeroTL = 1; 
        else
            datainiTL = datestr(data1,'yyyy-mm-dd');
            tempoiniTL = datestr(data1,'HH:MM:SS');
            NuovoZeroTL = 0;
        end
    end
    
    if NuovoZeroTL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiTL) < datenum(date)
                datainiTL = date;
            end
            NTiltLink = cell2mat(NodoTiltLink(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 16; % contatore colonne
            Condition = 0;
            while ii <= rTL
                ini_col = col-15;
                nN = num2str(NTiltLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTLd = curs.Data;
                % Giorni successivi
                 comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTL = curs.Data;
                [~,c1] = size(DATnodoTL);
                [~,c2] = size(DATnodoTLd);
                if c1 == c2
                    DATnodoTL = [DATnodoTLd; DATnodoTL];
                elseif c1 < c2 % solo giorno di riferimento
                    DATnodoTL = DATnodoTLd;
                end
                [rDAT,cDAT] = size(DATnodoTL);
                if rDAT==1 && cDAT==1
                    clear DatiElabTiltLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTiltLink);
                       if rDE ~= rDAT
                          clear DatiElabTiltLink
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTiltLink(:,ini_col:col) = DATnodoTL(:,:);
                end
                col = col+16;
                ii = ii+1;
            end
            if Condition == 1
                datainiTL = datenum(datainiTL) - 1;
                Data_scarico = datestr(datainiTL,'yyyy-mm-dd HH:MM:SS');
                datainiTL = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTiltLink(1,3:16:end)));
                for jj = 1:rTL
                    if Check == 1
                        datainiTL = datenum(datainiTL) - 1;
                        Data_scarico = datestr(datainiTL,'yyyy-mm-dd HH:MM:SS');
                        datainiTL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Tilt Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTiltLink = [];
    end
else
    datainiTL = [];
    tempoiniTL = [];
    DatiElabTiltLink = [];
    NuovoZeroTL = 0;
    margine = [];
end

%% Tilt Link HD V
if  yesHD == 1 % Importo ultimo dato elaborato dei Tilt Link HD V
    NodeNumHD = num2str(cell2mat(NodoTiltLinkHD(1,2))); % scarico i dati del primo nodo Tilt Link HD V
    NodeType = 'Tilt Link HD';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumHD ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumHD ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo

    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum = '  NodeNumHD ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiHD = datestr(data1,'yyyy-mm-dd');
        tempoiniHD = datestr(data1,'HH:MM:SS');
        NuovoZeroHD = 0;
        DatiElabTiltLinkHD = [];
        margine = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiHD = datestr(data2,'yyyy-mm-dd');
            tempoiniHD = datestr(data2,'HH:MM:SS');
            NuovoZeroHD = 1; 
        else
            datainiHD = datestr(data1,'yyyy-mm-dd');
            tempoiniHD = datestr(data1,'HH:MM:SS');
            NuovoZeroHD = 0;
        end
    end
    
    if NuovoZeroHD == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiHD) < datenum(date)
                datainiHD = date;
            end
            NTiltLinkHD = cell2mat(NodoTiltLinkHD(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 16; % contatore colonne
            Condition = 0;
            while ii <= rHD
                ini_col = col-15;
                nN = num2str(NTiltLinkHD(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoHDd = curs.Data;
                % Giorni successivi
                 comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoHD = curs.Data;
                [~,c1] = size(DATnodoHD);
                [~,c2] = size(DATnodoHDd);
                if c1 == c2
                    DATnodoHD = [DATnodoHDd; DATnodoHD];
                elseif c1 < c2 % solo giorno di riferimento
                    DATnodoHD = DATnodoHDd;
                end
                [rDAT,cDAT] = size(DATnodoHD);
                if rDAT==1 && cDAT==1
                    clear DatiElabTiltLinkHD
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTiltLinkHD);
                       if rDE ~= rDAT
                          clear DatiElabTiltLinkHD
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTiltLinkHD(:,ini_col:col) = DATnodoHD(:,:);
                end
                col = col+16;
                ii = ii+1;
            end
            if Condition == 1
                datainiHD = datenum(datainiHD) - 1;
                Data_scarico = datestr(datainiHD,'yyyy-mm-dd HH:MM:SS');
                datainiHD = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTiltLinkHD(1,3:16:end)));
                for jj = 1:rHD
                    if Check == 1
                        datainiHD = datenum(datainiHD) - 1;
                        Data_scarico = datestr(datainiHD,'yyyy-mm-dd HH:MM:SS');
                        datainiHD = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Tilt Link HD VR executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTiltLinkHD = [];
    end
else
    datainiHD = [];
    tempoiniHD = [];
    DatiElabTiltLinkHD = [];
    NuovoZeroHD = 0;
end

%% Tilt Link HD VR
if  yesHDVR == 1 % Importo ultimo dato elaborato dei Tilt Link HD VR
    NodeNumHDVR = num2str(cell2mat(NodoTiltLinkHDVR(1,2))); % scarico i dati del primo nodo Tilt Link HD VR
    NodeType = 'Tilt Link HD VR';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumHDVR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumHDVR ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo

    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum = '  NodeNumHDVR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiHDVR = datestr(data1,'yyyy-mm-dd');
        tempoiniHDVR = datestr(data1,'HH:MM:SS');
        NuovoZeroHDVR = 0;
        DatiElabTiltLinkHDVR = [];
        margine = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiHDVR = datestr(data2,'yyyy-mm-dd');
            tempoiniHDVR = datestr(data2,'HH:MM:SS');
            NuovoZeroHDVR = 1; 
        else
            datainiHDVR = datestr(data1,'yyyy-mm-dd');
            tempoiniHDVR = datestr(data1,'HH:MM:SS');
            NuovoZeroHDVR = 0;
        end
    end
    
    if NuovoZeroHDVR == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiHDVR) < datenum(date)
                datainiHDVR = date;
            end
            NTiltLinkHDVR = cell2mat(NodoTiltLinkHDVR(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 16; % contatore colonne
            Condition = 0;
            while ii <= rHDVR
                ini_col = col-15;
                nN = num2str(NTiltLinkHDVR(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoHDVRd = curs.Data;
                % Giorni successivi
                 comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoHDVR = curs.Data;
                [~,c1] = size(DATnodoHDVR);
                [~,c2] = size(DATnodoHDVRd);
                if c1 == c2
                    DATnodoHDVR = [DATnodoHDVRd; DATnodoHDVR];
                elseif c1 < c2 % solo giorno di riferimento
                    DATnodoHDVR = DATnodoHDVRd;
                end
                [rDAT,cDAT] = size(DATnodoHDVR);
                if rDAT==1 && cDAT==1
                    clear DatiElabTiltLinkHDVR
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTiltLinkHDVR);
                       if rDE ~= rDAT
                          clear DatiElabTiltLinkHD
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTiltLinkHDVR(:,ini_col:col) = DATnodoHDVR(:,:);
                end
                col = col+16;
                ii = ii+1;
            end
            if Condition == 1
                datainiHDVR = datenum(datainiHDVR) - 1;
                Data_scarico = datestr(datainiHDVR,'yyyy-mm-dd HH:MM:SS');
                datainiHDVR = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTiltLinkHDVR(1,3:16:end)));
                for jj = 1:rHDVR
                    if Check == 1
                        datainiHDVR = datenum(datainiHDVR) - 1;
                        Data_scarico = datestr(datainiHDVR,'yyyy-mm-dd HH:MM:SS');
                        datainiHDVR = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Tilt Link HD VR executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTiltLinkHDVR = [];
    end
else
    datainiHDVR = [];
    tempoiniHDVR = [];
    DatiElabTiltLinkHDVR = [];
    NuovoZeroHDVR = 0;
end

%% Tilt Link HR V
if  yesTLHR == 1 % Importo ultimo dato elaborato dei Tilt Link HR V
    NodeNumTLHR = num2str(cell2mat(NodoTiltLinkHR(1,2))); % scarico i dati del primo nodo Tilt Link HR V
    NodeType = 'Tilt Link HR';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && cI == 1
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLHR ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo

    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiTLHR = datestr(data1,'yyyy-mm-dd');
        tempoiniTLHR = datestr(data1,'HH:MM:SS');
        NuovoZeroTLHR = 0;
        DatiElabTiltLinkHR = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;  
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst_HR > Ndati
            if margine < Wdevst_HR/2
                margine = round(Wdevst_HR/2+1);
            end
            cont = Wdevst_HR + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiTLHR = datestr(data2,'yyyy-mm-dd');
            tempoiniTLHR = datestr(data2,'HH:MM:SS');
            NuovoZeroTLHR = 1; %1
        else
            datainiTLHR = datestr(data1,'yyyy-mm-dd');
            tempoiniTLHR = datestr(data1,'HH:MM:SS');
            NuovoZeroTLHR = 0;
        end
    end
    
    if NuovoZeroTLHR == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiTLHR) < datenum(date)
                datainiTLHR = date;
            end
            NTiltLinkHR = cell2mat(NodoTiltLinkHR(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 16; % contatore colonne
            Condition = 0;
            while ii <= rTLHR
                  ini_col = col-15;
                  nN = num2str(NTiltLinkHR(ii,1));
                  % Scarico di dati di spostamenti
                  comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                  curs = exec(conn,comando);
                  curs = fetch(curs);
                  DATnodoTLHRd = curs.Data;
                  % Scarico di dati di spostamenti
                  comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                  curs = exec(conn,comando);
                  curs = fetch(curs);
                  DATnodoTLHR = curs.Data;
                  [~,c1] = size(DATnodoTLHR);
                  [~,c2] = size(DATnodoTLHRd);
                  if c1 == c2
                     DATnodoTLHR = [DATnodoTLHRd; DATnodoTLHR];
                  elseif c1 < c2 % solo giorno di riferimento
                     DATnodoTLHR = DATnodoTLHRd;
                  end
                  [rDAT,cDAT] = size(DATnodoTLHR);
                  if rDAT==1 && cDAT==1
                      clear DatiElabTiltLinkHR
                      fclose(fileID);
                      cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                      Condition = 1;
                      break
                  else
                      if ii > 1 % Nodo successivo al primo
                          [rDE,~] = size(DatiElabTiltLinkHR);
                          if rDE ~= rDAT
                              clear DatiElabTiltLinkHR
                              fclose(fileID);
                              cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                              Condition = 1;
                              break
                          end
                      end
                      DatiElabTiltLinkHR(:,ini_col:col) = DATnodoTLHR(:,:);
                  end
                  col = col+16;
                  ii = ii+1;
            end
            if Condition == 1
                datainiTLHR = datenum(datainiTLHR) - 1;
                Data_scarico = datestr(datainiTLHR,'yyyy-mm-dd HH:MM:SS');
                datainiTLHR = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTiltLinkHR(1,3:16:end)));
                for jj = 1:rTLHR
                    if Check == 1
                        datainiTLHR = datenum(datainiTLHR) - 1;
                        Data_scarico = datestr(datainiTLHR,'yyyy-mm-dd HH:MM:SS');
                        datainiTLHR = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end  
        end
        text = 'Tilt Link HR executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTiltLinkHR = [];
    end
else
    datainiTLHR = [];
    tempoiniTLHR = [];
    DatiElabTiltLinkHR = [];
    NuovoZeroTLHR = 0;
end

%% Tilt Link H
if  yesTLH == 1 % Importo ultimo dato elaborato dei Tilt Link H
    NodeNumTLH = num2str(cell2mat(NodoTiltLinkH(1,2))); % scarico i dati del primo nodo Tilt Link H
    NodeType = 'Tilt Link H';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLH ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLH ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLH ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiTLH = datestr(data1,'yyyy-mm-dd');
        tempoiniTLH = datestr(data1,'HH:MM:SS');
        NuovoZeroTLH = 0;
        DatiElabTiltLinkH = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;    
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiTLH = datestr(data2,'yyyy-mm-dd');
            tempoiniTLH = datestr(data2,'HH:MM:SS');
            NuovoZeroTLH = 1;
        else
            datainiTLH = datestr(data1,'yyyy-mm-dd');
            tempoiniTLH = datestr(data1,'HH:MM:SS');
            NuovoZeroTLH = 0;
        end
    end
    
    if NuovoZeroTLH == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiTLH) < datenum(date)
                datainiTLH = date;
            end
            NTiltLinkH = cell2mat(NodoTiltLinkH(:,2));
            ii = 1; % contatore
            col = 13; % contatore colonne
            Condition = 0;
            while ii <= rTLH
                ini_col = col-12;
                nN = num2str(NTiltLinkH(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTLHd = curs.Data;
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTLH = curs.Data;
                [~,c1] = size(DATnodoTLH);
                [~,c2] = size(DATnodoTLHd);
                if c1 == c2
                   DATnodoTLH = [DATnodoTLHd; DATnodoTLH];
                elseif c1 < c2 % solo giorno di riferimento
                       DATnodoTLH = DATnodoTLHd;
                end
                [rDAT,cDAT] = size(DATnodoTLH);
                if rDAT==1 && cDAT==1
                    if strcmp(IDcentralina,'ID0174')==0
                        clear DatiElabTiltLinkH
                        fclose(fileID);
                        cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                        Condition = 1;
                        break
                    end
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTiltLinkH);
                       if rDE ~= rDAT
                           if strcmp(IDcentralina,'ID0174')==0
                               clear DatiElabTiltLinkH
                               fclose(fileID);
                              cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                              Condition = 1;
                              break
                          end
                       end
                    end
                    DatiElabTiltLinkH(:,ini_col:col) = DATnodoTLH(:,:);
                end
                col = col+13;
                ii = ii+1;
            end
            if Condition == 1
                datainiTLH = datenum(datainiTLH) - 1;
                Data_scarico = datestr(datainiTLH,'yyyy-mm-dd HH:MM:SS');
                datainiTLH = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTiltLinkH(1,5:13:end)));
                for jj = 1:rTLH
                    if Check == 1
                        datainiTLH = datenum(datainiTLH) - 1;
                        Data_scarico = datestr(datainiTLH,'yyyy-mm-dd HH:MM:SS');
                        datainiTLH = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Tilt Link H executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTiltLinkH = [];
    end
else
    datainiTLH = [];
    tempoiniTLH = [];
    DatiElabTiltLinkH = [];
    NuovoZeroTLH = 0;
end

%% Tilt Link HR H
if  yesTLHRH == 1 % Importo ultimo dato elaborato dei Tilt Link HR H
    NodeNumTLHRH = num2str(cell2mat(NodoTiltLinkHRH(1,2))); % scarico i dati del primo nodo Tilt Link HR H
    NodeType = 'Tilt Link HR H';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLHRH ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLHRH ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTLHRH ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiTLHRH = datestr(data1,'yyyy-mm-dd');
        tempoiniTLHRH = datestr(data1,'HH:MM:SS');
        NuovoZeroTLHRH = 0;
        DatiElabTiltLinkHRH = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst_HR > Ndati
            if margine < Wdevst_HR/2
                margine = round(Wdevst_HR/2+1);
            end
            cont = Wdevst_HR + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiTLHRH = datestr(data2,'yyyy-mm-dd');
            tempoiniTLHRH = datestr(data2,'HH:MM:SS');
            NuovoZeroTLHRH = 1; %1
        else
            datainiTLHRH = datestr(data1,'yyyy-mm-dd');
            tempoiniTLHRH = datestr(data1,'HH:MM:SS');
            NuovoZeroTLHRH = 0;
        end
    end
    
    if NuovoZeroTLHRH == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiTLHRH) < datenum(date)
                datainiTLHRH = date;
            end
            NTiltLinkHRH = cell2mat(NodoTiltLinkHRH(:,2));
            ii = 1; % contatore
            col = 13; % contatore colonne
            Condition = 0;
            while ii <= rTLHRH
                ini_col = col-12;
                nN = num2str(NTiltLinkHRH(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTLHRHd = curs.Data;
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoTLHRH = curs.Data;
                [~,c1] = size(DATnodoTLHRH);
                [~,c2] = size(DATnodoTLHRHd);
                if c1 == c2
                   DATnodoTLHRH = [DATnodoTLHRHd; DATnodoTLHRH];
                elseif c1 < c2 % solo giorno di riferimento
                     DATnodoTLHRH = DATnodoTLHRHd;
                end
                [rDAT,cDAT] = size(DATnodoTLHRH);
                if rDAT==1 && cDAT==1
                    clear DatiElabTiltLinkHRH
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTiltLinkHRH);
                       if rDE ~= rDAT
                          clear DatiElabTiltLinkHRH
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTiltLinkHRH(:,ini_col:col) = DATnodoTLHRH(:,:);
                end
                col = col+13;
                ii = ii+1;
            end
            if Condition == 1
                datainiTLHRH = datenum(datainiTLHRH) - 1;
                Data_scarico = datestr(datainiTLHRH,'yyyy-mm-dd HH:MM:SS');
                datainiTLHRH = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,5:13:end)));
                for jj = 1:rTLHRH
                    if Check == 1
                        datainiTLHRH = datenum(datainiTLHRH) - 1;
                        Data_scarico = datestr(datainiTLHRH,'yyyy-mm-dd HH:MM:SS');
                        datainiTLHRH = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end  
        end
        text = 'Tilt Link HR H executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTiltLinkHRH = [];
    end
else
    datainiTLHRH = [];
    tempoiniTLHRH = [];
    DatiElabTiltLinkHRH = [];
    NuovoZeroTLHRH = 0;
end

%% Piezo Link
if  yesPL == 1 % Importo ultimo dato elaborato dei Piezo Link
    NodeNumPL = num2str(cell2mat(NodoPiezoLink(1,2))); % scarico i dati del primo nodo Piezo Link
    NodeType = 'Piezo Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiPL = datestr(data1,'yyyy-mm-dd');
        tempoiniPL = datestr(data1,'HH:MM:SS');
        NuovoZeroPL = 0;
        marginePiezo = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        marginePiezo = 1;
        if Ndatidespike > NdatiMediaP
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMediaP;
        end
        if Wdevst > Ndati
            if marginePiezo < Wdevst/2
                marginePiezo = round(Wdevst/2+1);
            end
            cont = Wdevst + marginePiezo;
        else
            if marginePiezo < Ndati/2
                marginePiezo = round(Ndati/2+1);
            end
            cont = Ndati + marginePiezo;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiPL = datestr(data2,'yyyy-mm-dd');
            tempoiniPL = datestr(data2,'HH:MM:SS');
            NuovoZeroPL = 1;
        else
            datainiPL = datestr(data1,'yyyy-mm-dd');
            tempoiniPL = datestr(data1,'HH:MM:SS');
            NuovoZeroPL = 0;
        end
    end
    text = 'Piezo Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiPL = [];
    tempoiniPL = [];
    NuovoZeroPL = 0;
    marginePiezo = [];
end

%% Baro Link
if strcmp(NodoPiezoLink(1,4),'VW kPa') == 1 || strcmp(NodoPiezoLink(1,4),'VW kg/cm2') == 1
elseif yesBL == 0 && yesPL == 1
   % carico le informazioni relative al numero e alla profondità per
   % BARO LINK recuperato
   IDunitBaro = Barometri(:,2);
   DTcatenaBaro = Barometri(:,1);
   [rIB,~] = size(IDunitBaro);
   unitID = cell(rIB,1);
   idToolB = cell(rIB,1);
   Leggo = cell(rIB,2);
   for b = 1:rIB
       comando = ['select id from units where name = ''' char(IDunitBaro(b,1)) ''' '];
       curs = exec(conn,comando); 
       curs = fetch(curs);
       unitID(b,1) = cellstr(num2str(cell2mat(curs.Data)));
    
       comando = ['select id from tools where name = '''  char(DTcatenaBaro(b,1)) ''' and unit_id = '''...
           char(unitID(b,1)) ''' '];
       curs = exec(conn,comando);
       curs = fetch(curs);
       idToolB(b,1) = cellstr(num2str(cell2mat(curs.Data)));
   
       comando = ['select num, depth from nodes where tool_id = '''  char(idToolB(b,1)) ''' and nodetype_id = 3'];
       curs = exec(conn,comando);
       curs = fetch(curs);
       Leggo(b,:) = cell(curs.Data);
   end
   % cambio il segno della profondità (nel DB è positivo)
   depthB = cell2mat(Leggo(:,2));
   depthB = (-1)*depthB;
   depthB = num2cell(depthB);
   [r,~] = size(depthB);
   nome = cell(r,1);
   for i=1:r 
       nome(i,1) = cellstr('Baro Link');
   end
   NodoBaroLink = [nome Leggo(:,1) depthB];      
end

if  yesBL == 1 % Importo ultimo dato elaborato dei Baro Link
    NodeNumBL = num2str(cell2mat(NodoBaroLink(1,2))); % scarico i dati del primo nodo Baro Link
    NodeType = 'Baro Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumBL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
            date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumBL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumBL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
end
   
if strcmp(NodoPiezoLink(1,4),'VW kPa') == 1 || strcmp(NodoPiezoLink(1,4),'VW kg/cm2') == 1
    Barometro = [];
    % Non ho il barometro, ma ho il piezometro
elseif yesBL == 0 && yesPL == 1
    % Cerco se ci sono dati del barometro in corrispondenza di datainiPL
    NodeType = 'Baro Link';
    option = 0;
    for b = 1:rIB
        IDcentralinaBaro = char(Barometri(b,2));
        DTcatenaBaro = char(Barometri(b,1));
        NodeNumBL = cellstr(num2str(cell2mat(Leggo(b,1))));
        % Dati Grezzi
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            datainiPL ''' and time >= ''' tempoiniPL ''' and UnitName = ''' IDcentralinaBaro ''' and ToolNameID = '''...
            DTcatenaBaro ''' and NodeType = ''' NodeType ''' and NodeNum ='  char(NodeNumBL) ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
        if rI == 1 && cI == 1
        else
            Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
            option = 0;
            break
        end
    end
    [rI,cI] = size(D_num_ini);
    if rI == 1 && cI == 1 % Se in nessuno dei barometri ho trovato dati per
        % quella data, cerco una data precedente e prendo la maggiore
        for b = 1:rIB
            IDcentralinaBaro = char(Barometri(b,2));
            DTcatenaBaro = char(Barometri(b,1));
            NodeNumBL = cellstr(num2str(cell2mat(Leggo(b,1))));
            % Dati Grezzi
            comando = ['select Date, Time from RawDataView where Date < ''' ...
                datainiPL ''' and UnitName = ''' IDcentralinaBaro ''' and ToolNameID = '''...
                DTcatenaBaro ''' and NodeType = ''' NodeType ''' and NodeNum ='  char(NodeNumBL) ' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            D_num_ini = curs.Data;
            [rI,cI] = size(D_num_ini);
            if rI == 1 && cI == 1
            else
                Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
                option = 1;
                break
            end
        end
    end
    
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    Barometro = Barometri(b,:);
    if option == 0
        dataBaro = Data_num_ini(1);
    elseif option == 1
        dataBaro = Data_num_ini(end);
    end
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    if dataBaro>data1a
        data1 = dataBaro;
    else
        data1 = data1a;
    end
    rLE = 2; % forzo il parametro per il passaggio successivo
    cLE = 2; % forzo il parametro per il passaggio successivo
else
    Barometro = [];
end

if yesBL == 1 || yesPL == 1
    if rLE==1 && cLE==1
        datainiBL = datestr(data1,'yyyy-mm-dd');
        tempoiniBL = datestr(data1,'HH:MM:SS');
        NuovoZeroBL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        if strcmp(char(Data_LastElab),'No Data') == 0
            Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
            Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
            marginePiezo = 1;
            if Ndatidespike > NdatiMediaP
                Ndati = Ndatidespike;
            else
                Ndati = NdatiMediaP;
            end
            if Wdevst > Ndati
                if marginePiezo < Wdevst/2
                    marginePiezo = round(Wdevst/2+1);
                end
                cont = Wdevst + marginePiezo;
            else
                if marginePiezo < Ndati/2
                    marginePiezo = round(Ndati/2+1);
                end
                cont = Ndati + marginePiezo;
            end
            if cont < round(Ndatidespike/2)
                cont = round(Ndatidespike/2);
            end
            [rDS,~] = size(Data_numero);
            if rDS <= cont
                cont = rDS-1;
            end
            Data_scarico =  Data_numero(end-cont);
            Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
            data2 = datenum(Data_scarico);
            if data2 > data1
                datainiBL = datestr(data2,'yyyy-mm-dd');
                tempoiniBL = datestr(data2,'HH:MM:SS');
                NuovoZeroBL = 1; %1
            else
                datainiBL = datestr(data1,'yyyy-mm-dd');
                tempoiniBL = datestr(data1,'HH:MM:SS');
                NuovoZeroBL = 0;
            end
        else
            datainiBL = datestr(data1,'yyyy-mm-dd');
            tempoiniBL = datestr(data1,'HH:MM:SS');
            NuovoZeroBL = 0;
        end
    end
    if yesBL == 0 && yesPL == 1 
       dataBL = datenum(datainiBL);
       dataPL = datenum(datainiPL);
       if dataBL > dataPL % la data di primo scarico del barometro è maggiore del piezometro. Per l'elaborazione deve essere almeno il contrario
          datainiBL = datainiPL;
       end
    end
    text = 'Baro Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiBL = [];
    tempoiniBL = [];
    NuovoZeroBL = 0;
    Barometro = [];
end

%% Therm Link 
if  yesThL == 1 % Importo ultimo dato elaborato dei Therm Link
    NodeNumThL = num2str(cell2mat(NodoThermLink(1,2))); % scarico i dati del primo nodo Therm Link
    NodeType = 'Therm Link HR';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumThL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumThL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumThL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiThL = datestr(data1,'yyyy-mm-dd');
        tempoiniThL = datestr(data1,'HH:MM:SS');
        NuovoZeroThL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiThL = datestr(data2,'yyyy-mm-dd');
            tempoiniThL = datestr(data2,'HH:MM:SS');
            NuovoZeroThL = 1;
        else
            datainiThL = datestr(data1,'yyyy-mm-dd');
            tempoiniThL = datestr(data1,'HH:MM:SS');
            NuovoZeroThL = 0;
        end
    end
    text = 'Therm Link HR executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiThL = [];
    tempoiniThL = [];
    NuovoZeroThL = 0;
end

%% Load Link
if  yesLL == 1 % Importo ultimo dato elaborato dei Load Link
    NodeNumLL = num2str(cell2mat(NodoLoadLink(1,2))); % scarico i dati del primo nodo Load Link
    NodeType = 'Load Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumLL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumLL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end  
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumLL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiLL = datestr(data1,'yyyy-mm-dd');
        tempoiniLL = datestr(data1,'HH:MM:SS');
        NuovoZeroLL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiLL = datestr(data2,'yyyy-mm-dd');
            tempoiniLL = datestr(data2,'HH:MM:SS');
            NuovoZeroLL = 1; %1
        else
            datainiLL = datestr(data1,'yyyy-mm-dd');
            tempoiniLL = datestr(data1,'HH:MM:SS');
            NuovoZeroLL = 0;
        end
    end
    text = 'Load Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiLL = [];
    tempoiniLL = [];
    NuovoZeroLL = 0;
end

%% Rain Link
if  yesRL == 1 % Importo ultimo dato elaborato dei Rain Link
    NodeNumRL = num2str(cell2mat(NodoRainLink(1,2))); % scarico i dati del primo nodo Rain Link
    NodeType = 'Rain Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumRL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumRL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumRL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiRL = datestr(data1,'yyyy-mm-dd');
        tempoiniRL = datestr(data1,'HH:MM:SS');
        NuovoZeroRL = 0;
        DatiElabRainLink = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiRL = datestr(data2,'yyyy-mm-dd');
            tempoiniRL = datestr(data2,'HH:MM:SS');
            NuovoZeroRL = 1; %1
        else
            datainiRL = datestr(data1,'yyyy-mm-dd');
            tempoiniRL = datestr(data1,'HH:MM:SS');
            NuovoZeroRL = 0;
        end
    end
    
    if NuovoZeroRL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiRL) < datenum(date)
                datainiRL = date;
            end
            NRainLink = cell2mat(NodoRainLink(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 4; % contatore colonne
            Condition = 0;
            while ii <= rRL
                ini_col = col-3;
                nN = num2str(NRainLink(ii,1));
                % Scarico di dati di pioggia
                comando = ['select EventDate, EventTime, ZShift, Z from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRLd = curs.Data;
                % Scarico di dati di pioggia
                comando = ['select EventDate, EventTime, ZShift, Z from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRL = curs.Data;
                [~,c1] = size(DATnodoRL);
                [~,c2] = size(DATnodoRLd);
                if c1 == c2
                   DATnodoRL = [DATnodoRLd; DATnodoRL];
                elseif c1 < c2 % solo giorno di riferimento
                       DATnodoRL = DATnodoRLd;
                end
                [rDAT,cDAT] = size(DATnodoRL);
                if rDAT==1 && cDAT==1
                    clear DatiElabRainLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                        [rDE,~] = size(DatiElabRainLink);
                        if rDE ~= rDAT
                            clear DatiElabRainLink
                            fclose(fileID);
                            cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                            Condition = 1;
                            break
                        end
                    end
                    DatiElabRainLink(:,ini_col:col) = DATnodoRL(:,:);
                end
                col = col+4;
                ii = ii+1;
            end
            if Condition == 1
                datainiRL = datenum(datainiRL) - 1;
                Data_scarico = datestr(datainiRL,'yyyy-mm-dd HH:MM:SS');
                datainiRL = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabRainLink(1,3:16:end)));
                for jj = 1:rRL
                    if Check == 1
                        datainiRL = datenum(datainiRL) - 1;
                        Data_scarico = datestr(datainiRL,'yyyy-mm-dd HH:MM:SS');
                        datainiRL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end  
        end
    else
        DatiElabRainLink = [];
    end
    text = 'Rain Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiRL = [];
    tempoiniRL = [];
    NuovoZeroRL = 0;
    DatiElabRainLink = [];
end

%% SPP Link
if  yesSPP == 1 % Importo ultimo dato elaborato dei SPP Link
    NodeNumSPP = num2str(cell2mat(NodoSPPLink(1,2))); % scarico i dati del primo nodo SPP Link
    NodeType = 'SPP Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSPP ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSPP ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSPP ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiSPP = datestr(data1,'yyyy-mm-dd');
        tempoiniSPP = datestr(data1,'HH:MM:SS');
        NuovoZeroSPP = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiSPP = datestr(data2,'yyyy-mm-dd');
            tempoiniSPP = datestr(data2,'HH:MM:SS');
            NuovoZeroSPP = 1;
        else
            datainiSPP = datestr(data1,'yyyy-mm-dd');
            tempoiniSPP = datestr(data1,'HH:MM:SS');
            NuovoZeroSPP = 0;
        end
    end
    text = 'SPP Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiSPP = [];
    tempoiniSPP = [];
    NuovoZeroSPP = [];
end

%% Klino Link
if  yesKL == 1 % Importo ultimo dato elaborato dei Klino Link
    NodeNumKL = num2str(cell2mat(NodoKlinoLink(rKL,2))); % scarico i dati del primo nodo Klino Link
    NodeType = 'Klino Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumKL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumKL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end  
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumKL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiKL = datestr(data1,'yyyy-mm-dd');
        tempoiniKL = datestr(data1,'HH:MM:SS');
        NuovoZeroKL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiKL = datestr(data2,'yyyy-mm-dd');
            tempoiniKL = datestr(data2,'HH:MM:SS');
            NuovoZeroKL = 1; %1
        else
            datainiKL = datestr(data1,'yyyy-mm-dd');
            tempoiniKL = datestr(data1,'HH:MM:SS');
            NuovoZeroKL = 0;
        end
    end
    text = 'Klino Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiKL = [];
    tempoiniKL = [];
    NuovoZeroKL = 0;
end

%% Klino Link HR
if  yesKLHR == 1 % Importo ultimo dato elaborato dei Klino Link
    NodeNumKLHR = num2str(cell2mat(NodoKlinoLinkHR(rKLHR,2))); % scarico i dati del primo nodo Klino Link HR
    NodeType = 'Klino Link HR';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumKLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumKLHR ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end  
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumKLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiKLHR = datestr(data1,'yyyy-mm-dd');
        tempoiniKLHR = datestr(data1,'HH:MM:SS');
        NuovoZeroKLHR = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst_HR > Ndati
            if margine < Wdevst_HR/2
                margine = round(Wdevst_HR/2+1);
            end
            cont = Wdevst_HR + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiKLHR = datestr(data2,'yyyy-mm-dd');
            tempoiniKLHR = datestr(data2,'HH:MM:SS');
            NuovoZeroKLHR = 1; %1
        else
            datainiKLHR = datestr(data1,'yyyy-mm-dd');
            tempoiniKLHR = datestr(data1,'HH:MM:SS');
            NuovoZeroKLHR = 0;
        end
    end
    text = 'Klino Link HR executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiKLHR = [];
    tempoiniKLHR = [];
    NuovoZeroKLHR = 0;
end

%% PT100 Link
if  yesPT100 == 1 % Importo ultimo dato elaborato dei PT100 Link
    [rPT100,~] = size(NodoPT100Link);
    NodeNumPT100 = num2str(cell2mat(NodoPT100Link(rPT100,2))); % scarico i dati del primo nodo PT100 Link
    NodeType = 'PT100 Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPT100 ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPT100 ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end 
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPT100 ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiPT100 = datestr(data1,'yyyy-mm-dd');
        tempoiniPT100 = datestr(data1,'HH:MM:SS');
        NuovoZeroPT100 = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiPT100 = datestr(data2,'yyyy-mm-dd');
            tempoiniPT100 = datestr(data2,'HH:MM:SS');
            NuovoZeroPT100 = 1;
        else
            datainiPT100 = datestr(data1,'yyyy-mm-dd');
            tempoiniPT100 = datestr(data1,'HH:MM:SS');
            NuovoZeroPT100 = 0;
        end
    end
    text = 'PT100 Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiPT100 = [];
    tempoiniPT100 = [];
    NuovoZeroPT100 = 0;
end

%% In Place Link
if  yesIPL == 1 % Importo ultimo dato elaborato dei In Place Link
    NodeNumIPL = num2str(cell2mat(NodoInPlaceLink(1,2))); % scarico i dati del primo nodo In Place Link
    NodeType = 'In Place Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumIPL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumIPL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo

    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumIPL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiIPL = datestr(data1,'yyyy-mm-dd');
        tempoiniIPL = datestr(data1,'HH:MM:SS');
        NuovoZeroIPL = 0;
        DatiElabInPlaceLink = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiIPL = datestr(data2,'yyyy-mm-dd');
            tempoiniIPL = datestr(data2,'HH:MM:SS');
            NuovoZeroIPL = 1; 
        else
            datainiIPL = datestr(data1,'yyyy-mm-dd');
            tempoiniIPL = datestr(data1,'HH:MM:SS');
            NuovoZeroIPL = 0;
        end
    end
    
    if NuovoZeroIPL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiIPL) < datenum(date)
                datainiIPL = date;
            end
            NInPlaceLink = cell2mat(NodoInPlaceLink(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 16; % contatore colonne
            Condition = 0;
            while ii <= rIPL
                ini_col = col-15;
                nN = num2str(NInPlaceLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoIPLd = curs.Data;
                 % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, '...
                    'HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local '...
                    'from ElabDataView where EventDate > ''' Data_scarico(1,1:10) ''' and  UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoIPL = curs.Data;
                [~,c1] = size(DATnodoIPL);
                [~,c2] = size(DATnodoIPLd);
                if c1 == c2
                   DATnodoIPL = [DATnodoIPLd; DATnodoIPL];
                elseif c1 < c2 % solo giorno di riferimento
                       DATnodoIPL = DATnodoIPLd;
                end
                [rDAT,cDAT] = size(DATnodoIPL);
                if rDAT==1 && cDAT==1
                    if strcmp(IDcentralina,'ID0146') == 0 || strcmp(IDcentralina,'ID0214') == 0 ...
                            || strcmp(IDcentralina,'ID0236') == 0
                        clear DatiElabInPlaceLink
                        fclose(fileID);
                        cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                        Condition = 1;
                        break
                    end
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabInPlaceLink);
                       if rDE ~= rDAT
                           if strcmp(IDcentralina,'ID0146') == 0 || strcmp(IDcentralina,'ID0214') == 0  ...
                                   || strcmp(IDcentralina,'ID0236') == 0
                               clear DatiElabInPlaceLink
                               fclose(fileID);
                               cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                               Condition = 1;
                               break
                           else
                               DATnodoIPL(end+1:rDE,:) = DATnodoIPL(end,:);
                           end
                       end
                    end
                    DatiElabInPlaceLink(:,ini_col:col) = DATnodoIPL(:,:);
                end
                col = col+16;
                ii = ii+1;
            end
            if Condition == 1
                datainiIPL = datenum(datainiIPL) - 1;
                Data_scarico = datestr(datainiIPL,'yyyy-mm-dd HH:MM:SS');
                datainiIPL = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabInPlaceLink(1,3:16:end)));
                for jj = 1:rIPL
                    if Check(1,jj) == 1
                        datainiIPL = datenum(datainiIPL) - 1;
                        Data_scarico = datestr(datainiIPL,'yyyy-mm-dd HH:MM:SS');
                        datainiIPL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'In Place Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabInPlaceLink = [];
    end
else
    datainiIPL = [];
    tempoiniIPL = [];
    DatiElabInPlaceLink = [];
    NuovoZeroIPL = 0;
end

%% In Place Link HR
if  yesIPLHR == 1 % Importo ultimo dato elaborato dei In Place Link HR
    NodeNumIPLHR = num2str(cell2mat(NodoInPlaceLinkHR(1,2))); % scarico i dati del primo nodo 
    NodeType = 'In Place Link HR';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumIPLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && cI == 1
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumIPLHR ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo

    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumIPLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiIPLHR = datestr(data1,'yyyy-mm-dd');
        tempoiniIPLHR = datestr(data1,'HH:MM:SS');
        NuovoZeroIPLHR = 0;
        DatiElabInPlaceLinkHR = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;  
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst_HR > Ndati
            if margine < Wdevst_HR/2
                margine = round(Wdevst_HR/2+1);
            end
            cont = Wdevst_HR + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiIPLHR = datestr(data2,'yyyy-mm-dd');
            tempoiniIPLHR = datestr(data2,'HH:MM:SS');
            NuovoZeroIPLHR = 1; %1
        else
            datainiIPLHR = datestr(data1,'yyyy-mm-dd');
            tempoiniIPLHR = datestr(data1,'HH:MM:SS');
            NuovoZeroIPLHR = 0;
        end
    end
    
    if NuovoZeroIPLHR == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiIPLHR) < datenum(date)
                datainiIPLHR = date;
            end
            NInPlaceLinkHR = cell2mat(NodoInPlaceLinkHR(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 16; % contatore colonne
            Condition = 0;
            while ii <= rIPLHR
                  ini_col = col-15;
                  nN = num2str(NInPlaceLinkHR(ii,1));
                  % Scarico di dati di spostamenti
                  comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                  curs = exec(conn,comando);
                  curs = fetch(curs);
                  DATnodoIPLHRd = curs.Data;
                  % Scarico di dati di spostamenti
                  comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, HShift, HShift_local, HShiftDir, T_node, speed, speed_local, acceleration, acceleration_local from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = '''...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                  curs = exec(conn,comando);
                  curs = fetch(curs);
                  DATnodoIPLHR = curs.Data;
                  [~,c1] = size(DATnodoIPLHR);
                  [~,c2] = size(DATnodoIPLHRd);
                  if c1 == c2
                     DATnodoIPLHR = [DATnodoIPLHRd; DATnodoIPLHR];
                  elseif c1 < c2 % solo giorno di riferimento
                         DATnodoIPLHR = DATnodoIPLHRd;
                  end
                  [rDAT,cDAT] = size(DATnodoIPLHR);
                  if rDAT==1 && cDAT==1
                      if strcmp(IDcentralina,'ID0146') == 0 || strcmp(IDcentralina,'ID0214') == 0 || ...
                              strcmp(IDcentralina,'ID0236') == 0
                          clear DatiElabInPlaceLinkHR
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          Condition = 1;
                          break
                      end
                  else
                      if ii > 1 % Nodo successivo al primo
                          [rDE,~] = size(DatiElabInPlaceLinkHR);
                          if rDE ~= rDAT
                              if strcmp(IDcentralina,'ID0146') == 0 || strcmp(IDcentralina,'ID0214') == 0 || ...
                                      strcmp(IDcentralina,'ID0236') == 0
                                  clear DatiElabInPlaceLinkHR
                                  fclose(fileID);
                                  cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                                  Condition = 1;
                                  break
                              else
                                  DATnodoIPLHR(end+1:rDE,:) = DATnodoIPLHR(end,:);
                              end
                          end
                      end
                      DatiElabInPlaceLinkHR(:,ini_col:col) = DATnodoIPLHR(:,:);
                  end
                  col = col+16;
                  ii = ii+1;
            end
            if Condition == 1
                datainiIPLHR = datenum(datainiIPLHR) - 1;
                Data_scarico = datestr(datainiIPLHR,'yyyy-mm-dd HH:MM:SS');
                datainiIPLHR = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabInPlaceLinkHR(1,3:16:end)));
                for jj = 1:rIPLHR
                    if Check(1,jj) == 1
                        datainiIPLHR = datenum(datainiIPLHR) - 1;
                        Data_scarico = datestr(datainiIPLHR,'yyyy-mm-dd HH:MM:SS');
                        datainiIPLHR = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end  
        end
        text = 'In Place Link HR executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabInPlaceLinkHR = [];
    end
else
    datainiIPLHR = [];
    tempoiniIPLHR = [];
    DatiElabInPlaceLinkHR = [];
    NuovoZeroIPLHR = 0;
end

%% Weir Link
if  yesWL == 1 % Importo ultimo dato elaborato dei Weir Link
    [rWL,~] = size(NodoWeirLink);
    NodeNumWeir = num2str(cell2mat(NodoWeirLink(rWL,2))); % scarico i dati del primo nodo Weir Link
    NodeType = 'Weir Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumWeir ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumWeir ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end 
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumWeir ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiWL = datestr(data1,'yyyy-mm-dd');
        tempoiniWL = datestr(data1,'HH:MM:SS');
        NuovoZeroWL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiWL = datestr(data2,'yyyy-mm-dd');
            tempoiniWL = datestr(data2,'HH:MM:SS');
            NuovoZeroWL = 1;
        else
            datainiWL = datestr(data1,'yyyy-mm-dd');
            tempoiniWL = datestr(data1,'HH:MM:SS');
            NuovoZeroWL = 0;
        end
    end
    text = 'Weir Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiWL = [];
    tempoiniWL = [];
    NuovoZeroWL = 0;
end

%% Pendulum
if  yesPE == 1 % Importo ultimo dato elaborato dei Pendulum
    [rPE,~] = size(NodoPendulum);
    NodeNumPE = num2str(cell2mat(NodoPendulum(rPE,2))); % scarico i dati del primo nodo Pendulum
    NodeType = 'Pendulum';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPE ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPE ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end 
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPE ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiPE = datestr(data1,'yyyy-mm-dd');
        tempoiniPE = datestr(data1,'HH:MM:SS');
        NuovoZeroPE = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiPE = datestr(data2,'yyyy-mm-dd');
            tempoiniPE = datestr(data2,'HH:MM:SS');
            NuovoZeroPE = 1;
        else
            datainiPE = datestr(data1,'yyyy-mm-dd');
            tempoiniPE = datestr(data1,'HH:MM:SS');
            NuovoZeroPE = 0;
        end
    end
    text = 'Pendulum executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiPE = [];
    tempoiniPE = [];
    NuovoZeroPE = 0;
end

%% Snow Link
if  yesSL == 1
    NodeNumSL = num2str(cell2mat(NodoSnowLink(1,2)));
    NodeType = 'Snow Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI && cI == 1  % Non ci sono dati per la data "date"
        comando = ['select Date, Time from RawDataView where Date > ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    if rI == 1 && cI == 1
        Data_num_ini=0;
    else
        Data_num_ini = [cell2mat(D_num_ini(:,1)) repmat(' ', [rI,1]) cell2mat(D_num_ini(:,2))];
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    % Dati elaborati
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))];
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiSL = datestr(data1,'yyyy-mm-dd');
        tempoiniSL = datestr(data1,'HH:MM:SS');
        NuovoZeroSL = 0;
        DatiElabSnowLink = [];
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if Wdevst > Ndati
            if margine < Wdevst/2
                margine = round(Wdevst/2+1);
            end
            cont = Wdevst + margine;
        else
            if margine < Ndati/2
                margine = round(Ndati/2+1);
            end
            cont = Ndati + margine;
        end
        if cont < round(Ndatidespike/2)
            cont = round(Ndatidespike/2);
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiSL = datestr(data2,'yyyy-mm-dd');
            tempoiniSL = datestr(data2,'HH:MM:SS');
            NuovoZeroSL = 1; %1
        else
            datainiSL = datestr(data1,'yyyy-mm-dd');
            tempoiniSL = datestr(data1,'HH:MM:SS');
            NuovoZeroSL = 0;
        end
    end
    
    if NuovoZeroSL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiSL) < datenum(date)
                datainiSL = date;
            end
            NSnowLink = cell2mat(NodoSnowLink(:,2));
            % mi serve almeno una settimana per calcolare i maggiori spostamenti settimanali
            ii = 1; % contatore
            col = 4; % contatore colonne
            Condition = 0;
            while ii <= rSL
                ini_col = col-3;
                nN = num2str(NSnowLink(ii,1));
                % Scarico di dati di pioggia
                comando = ['select EventDate, EventTime, ZShift, Z from ElabDataView where EventDate = ''' ...
                    Data_scarico(1,1:10) ''' and EventTime >= ''' Data_scarico(1,12:end) ''' and  UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoSLd = curs.Data;
                % Scarico di dati di pioggia
                comando = ['select EventDate, EventTime, ZShift, Z from ElabDataView where EventDate > ''' ...
                    Data_scarico(1,1:10) ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
                    DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoSL = curs.Data;
                [~,c1] = size(DATnodoSL);
                [~,c2] = size(DATnodoSLd);
                if c1 == c2
                   DATnodoSL = [DATnodoSLd; DATnodoSL];
                elseif c1 < c2 % solo giorno di riferimento
                       DATnodoSL = DATnodoSLd;
                end
                [rDAT,cDAT] = size(DATnodoSL);
                if rDAT==1 && cDAT==1
                    clear DatiElabSnowLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                        [rDE,~] = size(DatiElabSnowLink);
                        if rDE ~= rDAT
                            clear DatiElabSnowLink
                            fclose(fileID);
                            cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                            Condition = 1;
                            break
                        end
                    end
                    DatiElabSnowLink(:,ini_col:col) = DATnodoSL(:,:);
                end
                col = col+4;
                ii = ii+1;
            end
            if Condition == 1
                datainiSL = datenum(datainiSL) - 1;
                Data_scarico = datestr(datainiSL,'yyyy-mm-dd HH:MM:SS');
                datainiSL = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabSnowLink(1,3:16:end)));
                for jj = 1:rRL
                    if Check == 1
                        datainiSL = datenum(datainiSL) - 1;
                        Data_scarico = datestr(datainiSL,'yyyy-mm-dd HH:MM:SS');
                        datainiSL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end  
        end
    else
        DatiElabSnowLink = [];
    end
    text = 'Snow Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiSL = [];
    tempoiniSL = [];
    NuovoZeroSL = 0;
    DatiElabSnowLink = [];
end

text = 'LastElab function worked correctly';
fprintf(fileID,fmt,text);
fclose(fileID);
end