function [DatiElabTunnelLink,NuovoZeroTuL,ay_TuL,t_TuL,ay_TuL_AC,t_TuL_AC,...
     datainiTuL,tempoiniTuL,data1_AC,DatiElabRadialLink,NuovoZeroRL,datainiRL,tempoiniRL,...
     DatiElabTiltLinkH,NuovoZeroTLH,datainiTLH,tempoiniTLH,...
     DatiElabTiltLinkHRH,NuovoZeroTLHRH,datainiTLHRH,tempoiniTLHRH,...
     DatiElabPreConvLink,NuovoZeroPCL,datainiPCL,tempoiniPCL,...
     DatiElabPreConvLinkHR,NuovoZeroPCLHR,datainiPCLHR,tempoiniPCLHR,...
     DatiElabDistoMTLink,NuovoZeroDM,datainiDM,tempoiniDM,...
     datainiPL,tempoiniPL,NuovoZeroPrL,datainiLL,tempoiniLL,NuovoZeroLL,...
     datainiEL,tempoiniEL,Date_Rif_EL,NuovoZeroEL,...
     dataini3DEL,tempoini3DEL,Date_Rif_3DEL,NuovoZero3DEL,datainiWEL,tempoiniWEL,NuovoZeroWEL,...
     datainiMPBEL,tempoiniMPBEL,NuovoZeroMPBEL,DatiElabAnalogLink,NuovoZeroAL,datainiAL,tempoiniAL,...
     datainiCrL,tempoiniCrL,NuovoZeroCrL,dataini3DCrL,tempoini3DCrL,NuovoZero3DCrL,...
     dataini2DCrL,tempoini2DCrL,NuovoZero2DCrL, datainiSM,tempoiniSM,Date_Rif_SM,NuovoZeroSM,margine] = ...
     LastElab(conn,date,time,IDcentralina,DTcatena,Wdevst,NdatiMedia,Ndatidespike,...
     NodoTunnelLink,yesTuL,date_AC,time_AC,Nodo_AC,rTuL,...
     NodoRadialLink,yesRL,rRL,NodoTiltLinkH,yesTLH,rTLH,NodoTiltLinkHRH,yesTLHRH,rTLHRH,...
     NodoPreConvLink,yesPCL,rPCL,NodoPreConvLinkHR,yesPCLHR,rPCLHR,...
     NodoDistoMTLink,yesDM,rDM,NodoPressureLink,yesPL,...
     NodoLoadLink,yesLL,NodoExtensometerLink,yesEL,Nodo3DExtensometerLink,...
     yes3DEL,NodoWireExtensometerLink,yesWEL,NodoMultiPointRodExtensometer,...
     yesMPBEL,NodoAnalogLink,yesAL,rAL,NodoCrackLink,yesCrL,...
     Nodo3DCrackLink,yes3DCrL,Nodo2DCrackLink,yes2DCrL,NodoStressMeter,yesSM,FileName)

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

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

%% Tunnel Link
if  yesTuL == 1 % Importo ultimo dato elaborato dei Tunnel Link
    NodeNumTuL = num2str(cell2mat(NodoTunnelLink(1,2))); % scarico i dati del primo nodo Tunnel Link
    NodeType = 'Tunnel Link';
    
    % Dati Grezzi
    % --- Sezione ---
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumTuL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumTuL ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini = curs.Data;
        [rI,cI] = size(D_num_ini);
    end
    % --- Arco Rovescio ---
    if date_AC <= now
        comando = ['select Date, Time from RawDataView where Date = ''' ...
            date_AC ''' and Time >= ''' time_AC ''' and UnitName = ''' IDcentralina...
            ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  num2str(Nodo_AC) ' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        D_num_ini_AC = curs.Data;
        [rAC,cAC] = size(D_num_ini_AC);
        if rAC == 1 && cAC == 1 % Non ci sono dati per la data "date"
            comando = ['select Date, Time from RawDataView where Date > ''' ...
                date_AC ''' and UnitName = ''' IDcentralina...
                ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
                NodeType ''' and NodeNum ='  Nodo_AC ' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            D_num_ini_AC = curs.Data;
            [rAC,~] = size(D_num_ini_AC);
        end
    else
        rAC = rI;
    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 ='  NodeNumTuL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Data_LastElab = curs.Data;
    [rLE,cLE] = size(Data_LastElab);
      
    Date_AC = [cell2mat(cellstr((date_AC))) repmat(' ', [1,1]) cell2mat(cellstr(time_AC))];
    data1_AC = Date_AC;
    
    Date = [cell2mat(Data(1,1)) repmat(' ', [1,1]) cell2mat(Data(1,2))]; % Data definita sul sito
    data1a = datenum(Date); % Data definita sul sito
    data1b = Data_num_ini(1); % Data del primo dato grezzo
    % Nodi standard
    if data1b > data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    % Arco rovescio
    if datenum(data1_AC) < data1
        data1_AC = data1;
    end
    
    if rLE==1 && cLE==1
        datainiTuL = datestr(data1,'yyyy-mm-dd');
        tempoiniTuL = datestr(data1,'HH:MM:SS');
        NuovoZeroTuL = 0;
        DatiElabTunnelLink = [];
        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; % margine di n gg prima

        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiTuL = datestr(data2,'yyyy-mm-dd');
            tempoiniTuL = datestr(data2,'HH:MM:SS');
            NuovoZeroTuL = 1; %1
        else
            datainiTuL = datestr(data1,'yyyy-mm-dd');
            tempoiniTuL = datestr(data1,'HH:MM:SS');
            NuovoZeroTuL = 0;
        end
    end
    
    if NuovoZeroTuL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiTuL) < datenum(date)
                datainiTuL = date;
            end
            NTunnelLink = cell2mat(NodoTunnelLink(:,2));
            ii = 1; % contatore
            col = 16; % contatore colonne
            ay_TuL = zeros(rTuL,rI);
            ay_TuL_AC = zeros(rTuL,rAC);
            t_TuL = zeros(rTuL,rI);
            t_TuL_AC = zeros(rTuL,rAC);
            Condition = 0;
            while ii <= rTuL
                ini_col = col-15;
                nN = num2str(NTunnelLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, Xstar, Zstar, HShift, HShift_local, T_node, Area, speed_local, 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);
                DATnodoTuLd = curs.Data;
                % Scarico di dati di spostamenti
                 comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, Xstar, Zstar, HShift, HShift_local, T_node, Area, speed_local, 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);
                 DATnodoTuL = curs.Data;
                 [~,c1] = size(DATnodoTuL);
                 [~,c2] = size(DATnodoTuLd);
                 if c1 == c2
                     DATnodoTuL = [DATnodoTuLd; DATnodoTuL];
                 elseif c1 < c2 % solo giorno di riferimento
                     DATnodoTuL = DATnodoTuLd;
                 end
                [rDAT,cDAT] = size(DATnodoTuL);
                if rDAT==1 && cDAT==1
                    clear DatiElabTunnelLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTunnelLink);
                       if rDE ~= rDAT
                          clear DatiElabTunnelLink
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTunnelLink(:,ini_col:col) = DATnodoTuL(:,:);
                end
                col = col+16;
              
                % scarico ay della prima data: mi serve dopo!
                % Nodi Standard
                if ii < Nodo_AC
                    data1b = datestr(data1b,'yyyy-mm-dd');
                    time1b = time;
                    comando = ['select Val1, Val6, Date, Time from RawDataView where Date = ''' ...
                        data1b ''' and Time >= ''' time1b ''' and ToolNameID = ''' ...
                        DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                        NodeType ''' and NodeNum = ''' nN ''' '];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DATnodoAccY = curs.Data;
                    [rD,cD] = size(DATnodoAccY);
                    if cD > 1
                        Date = datenum([cell2mat(DATnodoAccY(:,3)) repmat(' ', [rD,1]) cell2mat(DATnodoAccY(:,4))]);
                        % Elimino eventuali date doppie
                        a = 2;
                        while a <= rD
                            if Date(a,1) == Date(a-1,1)
                                Date(a,:) = [];
                                DATnodoAccY(a,:) = [];
                                rD = rD-1;
                            else
                                a = a+1;
                            end
                        end
                        % ay
                        dato = cellstr(DATnodoAccY(1,1))';
                        ay_TuL(ii,:) = str2double(dato);
                        % T
                        dato = cellstr(DATnodoAccY(1,2))';
                        t_TuL(ii,:) = str2double(dato);
                    end                        
                else
                    % Nodi Arco Rovescio
                    data1c = datestr(date_AC,'yyyy-mm-dd');
                    if datenum(data1c) < now
                        time1c = time_AC;
                        comando = ['select Val1, Val6, Date, Time from RawDataView where Date = ''' ...
                            data1c ''' and Time >= ''' time1c ''' and ToolNameID = ''' ...
                            DTcatena ''' and UnitName = ''' IDcentralina ''' and NodeType = ''' ...
                            NodeType ''' and NodeNum = ''' nN ''' '];
                        curs = exec(conn,comando);
                        curs = fetch(curs);
                        DATnodoAccY = curs.Data;
                        [rD,cD] = size(DATnodoAccY);
                        if cD > 1
                            Date = datenum([cell2mat(DATnodoAccY(:,3)) repmat(' ', [rD,1]) cell2mat(DATnodoAccY(:,4))]);
                            % Elimino eventuali date doppie
                            a = 2;
                            while a <= rD
                                if Date(a,1) == Date(a-1,1)
                                    Date(a,:) = [];
                                    DATnodoAccY(a,:) = [];
                                    rD = rD-1;
                                else
                                    a = a+1;
                                end
                            end
                            % ay
                            dato = cellstr(DATnodoAccY(:,1))';
                            ay_TuL_AC(ii,:) = str2double(dato);
                            % T
                            dato = cellstr(DATnodoAccY(:,2))';
                            t_TuL_AC(ii,:) = str2double(dato);
                        end
                    else
                        ay_TuL_AC = ay_TuL;
                        t_TuL_AC = t_TuL;
                    end
                end
                ii = ii+1;
            end
            
            if Condition == 1
               datainiTuL = datenum(datainiTuL) - 1;
               Data_scarico = datestr(datainiTuL,'yyyy-mm-dd HH:MM:SS');
               datainiTuL = Data_scarico;
               fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTunnelLink(1,3:16:end)));
                for jj = 1:rTuL
                    if Check == 1
                        datainiTuL = datenum(datainiTuL) - 1;
                        Data_scarico = datestr(datainiTuL,'yyyy-mm-dd HH:MM:SS');
                        datainiTuL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Tunnel Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTunnelLink = [];
        ay_TuL = [];
        ay_TuL_AC = [];
        t_TuL = [];
        t_TuL_AC = [];
    end
else
    datainiTuL = [];
    tempoiniTuL = [];
    data1_AC = [];
    DatiElabTunnelLink = [];
    ay_TuL = [];
    t_TuL = [];
    ay_TuL_AC = [];
    t_TuL_AC = [];
    NuovoZeroTuL = 0;
    margine = [];
end

%% Radial Link
if  yesRL == 1 % Importo ultimo dato elaborato dei Radial Link
    NodeNumRL = num2str(cell2mat(NodoRadialLink(1,2))); % scarico i dati del primo nodo Radial Link
    NodeType = 'Radial Link';
    
    % Scarico il primo dato grezzo
    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 == 1 && 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 && 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
    
    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;
        DatiElabRadialLink = [];
        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; % margine 
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        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;
        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
            NRadialLink = cell2mat(NodoRadialLink(:,2));
            ii = 1; % contatore
            col = 9; % contatore colonne
            Condition = 0;
            while ii <= rRL
                ini_col = col-8;
                nN = num2str(NRadialLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node 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 spostamenti
                 comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node 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 DatiElabRadialLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabRadialLink);
                       if rDE ~= rDAT
                          clear DatiElabRadialLink
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabRadialLink(:,ini_col:col) = DATnodoRL(:,:);
                end
                ii = ii+1;
                col = col+9;
            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(DatiElabRadialLink(1,3:9: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
        text = 'Radial Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabRadialLink = [];
    end
else
    datainiRL = [];
    tempoiniRL = [];
    DatiElabRadialLink = [];
    NuovoZeroRL = 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';
    
    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 == 1 && 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 && 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
    
    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 = [];
        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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        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, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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
                    clear DatiElabTiltLinkH
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTiltLinkH);
                       if rDE ~= rDAT
                          clear DatiElabTiltLinkH
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTiltLinkH(:,ini_col:col) = DATnodoTLH(:,:);
                end
                ii = ii+1;
                col = col+13;
            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,3: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';
    
    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 == 1 && 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 && 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
    
    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 = [];
        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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        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;
        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 <= rTLH
                ini_col = col-12;
                nN = num2str(NTiltLinkHRH(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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 DatiElabTiltLinkHR
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    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);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTiltLinkHRH(:,ini_col:col) = DATnodoTLHRH(:,:);
                end
                ii = ii+1;
                col = col+13;
            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,3: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

%% PreConv Link
if  yesPCL == 1 % Importo ultimo dato elaborato dei PreConv Link
    NodeNumPCL = num2str(cell2mat(NodoPreConvLink(1,2))); % scarico i dati del primo nodo PreConvLink
    NodeType = 'PreConv Link';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum = '  NodeNumPCL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 = '  NodeNumPCL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum = '  NodeNumPCL ' '];
    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
        datainiPCL = datestr(data1,'yyyy-mm-dd');
        tempoiniPCL = datestr(data1,'HH:MM:SS');
        NuovoZeroPCL = 0;
        DatiElabPreConvLink = [];
        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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiPCL = datestr(data2,'yyyy-mm-dd');
            tempoiniPCL = datestr(data2,'HH:MM:SS');
            NuovoZeroPCL = 1;
        else
            datainiPCL = datestr(data1,'yyyy-mm-dd');
            tempoiniPCL = datestr(data1,'HH:MM:SS');
            NuovoZeroPCL = 0;
        end
    end
    
    if NuovoZeroPCL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiPCL) < datenum(date)
                datainiPCL = date;
            end
            NPreConvLink = cell2mat(NodoPreConvLink(:,2));
            ii = 1; % contatore
            col = 13; % contatore colonne
            Condition = 0;
            while ii <= rPCL
                ini_col = col-12;
                nN = num2str(NPreConvLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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);
                DATnodoPCLd = curs.Data;
                 % Scarico di dati di spostamenti
                 comando = ['select EventDate, EventTime, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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);
                 DATnodoPCL = curs.Data;
                 [~,c1] = size(DATnodoPCL);
                 [~,c2] = size(DATnodoPCLd);
                 if c1 == c2
                     DATnodoPCL = [DATnodoPCLd; DATnodoPCL];
                 elseif c1 < c2 % solo giorno di riferimento
                     DATnodoPCL = DATnodoPCLd;
                 end
                [rDAT,cDAT] = size(DATnodoPCL);
                if rDAT==1 && cDAT==1
                    clear DatiElabPreConvLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabPreConvLink);
                       if rDE ~= rDAT
                          clear DatiElabPreConvLink
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabPreConvLink(:,ini_col:col) = DATnodoPCL(:,:);
                end
                ii = ii+1;
                col = col+13;
            end
            if Condition == 1
               datainiPCL = datenum(datainiPCL) - 1;
               Data_scarico = datestr(datainiPCL,'yyyy-mm-dd HH:MM:SS');
               datainiPCL = Data_scarico;
               fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabPreConvLink(1,3:13:end)));
                for jj = 1:rPCL
                    if Check == 1
                        datainiPCL = datenum(datainiPCL) - 1;
                        Data_scarico = datestr(datainiPCL,'yyyy-mm-dd HH:MM:SS');
                        datainiPCL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'PreConv Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabPreConvLink = [];
    end
else
    datainiPCL = [];
    tempoiniPCL = [];
    DatiElabPreConvLink = [];
    NuovoZeroPCL = 0;
end
    
%% PreConv Link HR
if  yesPCLHR == 1 % Importo ultimo dato elaborato dei PreConv Link HR
    NodeNumPCLHR = num2str(cell2mat(NodoPreConvLinkHR(1,2))); % scarico i dati del primo nodo PreConvLinkHR
    NodeType = 'PreConvLink HR';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNumPCLHR ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumPCLHR ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumPCLHR ' '];
    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
        datainiPCLHR = datestr(data1,'yyyy-mm-dd');
        tempoiniPCLHR = datestr(data1,'HH:MM:SS');
        NuovoZeroPCLHR = 0;
        DatiElabPreConvLinkHR = [];
        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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiPCLHR = datestr(data2,'yyyy-mm-dd');
            tempoiniPCLHR = datestr(data2,'HH:MM:SS');
            NuovoZeroPCLHR = 1;
        else
            datainiPCLHR = datestr(data1,'yyyy-mm-dd');
            tempoiniPCLHR = datestr(data1,'HH:MM:SS');
            NuovoZeroPCLHR = 0;
        end
    end
    
    if NuovoZeroPCLHR == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiPCLHR) < datenum(date)
                datainiPCLHR = date;
            end
            NPreConvLinkHR = cell2mat(NodoPreConvLinkHR(:,2));
            ii = 1; % contatore
            col = 13; % contatore colonne
            Condition = 0;
            while ii <= rPCLHR
                ini_col = col-12;
                nN = num2str(NPreConvLinkHR(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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);
                DATnodoPCLHRd = curs.Data;
                 % Scarico di dati di spostamenti
                 comando = ['select EventDate, EventTime, YShift, ZShift, Y, Z, AlfaX, AlfaY, 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);
                 DATnodoPCLHR = curs.Data;
                 [~,c1] = size(DATnodoPCLHR);
                 [~,c2] = size(DATnodoPCLHRd);
                 if c1 == c2
                     DATnodoPCLHR = [DATnodoPCLHRd; DATnodoPCLHR];
                 elseif c1 < c2 % solo giorno di riferimento
                     DATnodoPCLHR = DATnodoPCLHRd;
                 end
                [rDAT,cDAT] = size(DATnodoPCLHR);
                if rDAT==1 && cDAT==1
                    clear DatiElabPreConvLinkHR
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabPreConvLinkHR);
                       if rDE ~= rDAT
                          clear DatiElabPreConvLinkHR
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabPreConvLinkHR(:,ini_col:col) = DATnodoPCLHR(:,:);
                end
                ii = ii+1;
                col = col+13;
            end
            if Condition == 1
               datainiPCLHR = datenum(datainiPCLHR) - 1;
               Data_scarico = datestr(datainiPCLHR,'yyyy-mm-dd HH:MM:SS');
               datainiPCLHR = Data_scarico;
               fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabPreConvLinkHR(1,3:13:end)));
                for jj = 1:rPCLHR
                    if Check == 1
                        datainiPCLHR = datenum(datainiPCLHR) - 1;
                        Data_scarico = datestr(datainiPCLHR,'yyyy-mm-dd HH:MM:SS');
                        datainiPCLHR = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'PreConv Link HR executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabPreConvLinkHR = [];
    end
else
    datainiPCLHR = [];
    tempoiniPCLHR = [];
    DatiElabPreConvLinkHR = [];
    NuovoZeroPCLHR = 0;
end

%% DistoMT Link
if  yesDM == 1 % Importo ultimo dato elaborato dei DistoMT Link
    NodeNumDM = num2str(cell2mat(NodoDistoMTLink(1,2))); % scarico i dati del primo nodo di questo tipo
    NodeType = 'DistoMT Link';
    
    % Dati Grezzi
    % --- Sezione ---
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ...
        ''' and NodeNum ='  NodeNumDM ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumDM ' '];
        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 ='  NodeNumDM ' '];
    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))]; % Data definita sul sito
    data1a = datenum(Date); % Data definita sul sito
    data1b = Data_num_ini(1); % Data del primo dato grezzo
    % Nodi standard
    if data1b > data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiDM = datestr(data1,'yyyy-mm-dd');
        tempoiniDM = datestr(data1,'HH:MM:SS');
        NuovoZeroDM = 0;
        DatiElabDistoMTLink = [];
        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; % margine di n gg prima

        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiDM = datestr(data2,'yyyy-mm-dd');
            tempoiniDM = datestr(data2,'HH:MM:SS');
            NuovoZeroDM = 1; %1
        else
            datainiDM = datestr(data1,'yyyy-mm-dd');
            tempoiniDM = datestr(data1,'HH:MM:SS');
            NuovoZeroDM = 0;
        end
    end
    
    if NuovoZeroDM == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiDM) < datenum(date)
                datainiDM = date;
            end
            NDistoMTLink = cell2mat(NodoDistoMTLink(:,2));
            ii = 1; % contatore
            col = 12; % contatore colonne
            Condition = 0;
            while ii <= rDM
                ini_col = col-11;
                nN = num2str(NDistoMTLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, ZShift, X, '...
                    'Z, HShift, T_node, speed_local, acceleration_local, AlfaX, AlfaY '...
                    '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);
                DATnodoDLd = curs.Data;
                % Scarico di dati di spostamenti
                 comando = ['select EventDate, EventTime, XShift, ZShift, X, '...
                    'Z, HShift, T_node, speed_local, acceleration_local, AlfaX, AlfaY '...
                    '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);
                 DATnodoDM = curs.Data;
                 [~,c1] = size(DATnodoDM);
                 [~,c2] = size(DATnodoDMd);
                 if c1 == c2
                     DATnodoDM = [DATnodoDMd; DATnodoDM];
                 elseif c1 < c2 % solo giorno di riferimento
                     DATnodoDM = DATnodoDMd;
                 end
                [rDAT,cDAT] = size(DATnodoDM);
                if rDAT==1 && cDAT==1
                    clear DatiElabDistoMTLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabDistoMTLink);
                       if rDE ~= rDAT
                          clear DatiElabDistoMTLink
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabDistoMTLink(:,ini_col:col) = DATnodoDM(:,:);
                end
                col = col+12;
                ii = ii+1;
            end
            
            if Condition == 1
               datainiDM = datenum(datainiDM) - 1;
               Data_scarico = datestr(datainiDM,'yyyy-mm-dd HH:MM:SS');
               datainiDM = Data_scarico;
               fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabDistoMTLink(1,3:16:end)));
                for jj = 1:rTuL
                    if Check == 1
                        datainiDM = datenum(datainiDM) - 1;
                        Data_scarico = datestr(datainiDM,'yyyy-mm-dd HH:MM:SS');
                        datainiDM = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'DistoMT Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabDistoMTLink = [];
    end
else
    datainiDM = [];
    tempoiniDM = [];
    DatiElabDistoMTLink = [];
    NuovoZeroDM = 0;
end

%% Pressure Link
if  yesPL == 1 % Importo ultimo dato elaborato dei Pressure Link
    NodeNumPL = num2str(cell2mat(NodoPressureLink(1,2))); % scarico i dati del primo nodo Pressure Link
    NodeType = 'Pressure Link';
    
    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 == 1 && 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 && 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
    
    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');
        margine = 0;
        NuovoZeroPrL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiPL = datestr(data2,'yyyy-mm-dd');
            tempoiniPL = datestr(data2,'HH:MM:SS');
        else
            datainiPL = datestr(data1,'yyyy-mm-dd');
            tempoiniPL = datestr(data1,'HH:MM:SS');
        end
        NuovoZeroPrL = 1;
    end
    text = 'Pressure Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiPL = [];
    tempoiniPL = [];
    NuovoZeroPrL = 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';
    
    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 == 1 && 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 && 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
    
    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');
        margine = 0;
        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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiLL = datestr(data2,'yyyy-mm-dd');
            tempoiniLL = datestr(data2,'HH:MM:SS');
        else
            datainiLL = datestr(data1,'yyyy-mm-dd');
            tempoiniLL = datestr(data1,'HH:MM:SS');
        end
        NuovoZeroLL = 1;
    end
    text = 'Load Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiLL = [];
    tempoiniLL = [];
    NuovoZeroLL = 0;
end

%% Extensometer Link
if  yesEL == 1 % Importo ultimo dato elaborato dei Extensometer Link
    NodeNumEL = num2str(cell2mat(NodoExtensometerLink(1,2))); % scarico i dati del primo nodo Extensometer Link
    NodeType = 'Extensometer 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 ='  NodeNumEL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumEL ' '];
        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 ='  NodeNumEL ' '];
    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))];
    Date_Rif_EL = Data_num_ini(1);
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        datainiEL = datestr(data1,'yyyy-mm-dd');
        tempoiniEL = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZeroEL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiEL = datestr(data2,'yyyy-mm-dd');
            tempoiniEL = datestr(data2,'HH:MM:SS');
        else
            datainiEL = datestr(data1,'yyyy-mm-dd');
            tempoiniEL = datestr(data2,'HH:MM:SS');
        end
        NuovoZeroEL = 1;
    end
    text = 'Extensometer Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiEL = [];
    tempoiniEL = [];
    Date_Rif_EL = [];
    NuovoZeroEL = 0;
end

%% 3D Extensometer Link
if  yes3DEL == 1 % Importo ultimo dato elaborato dei 3D Extensometer Link
    NodeNum3DEL = num2str(cell2mat(Nodo3DExtensometerLink(1,2))); % scarico i dati del primo nodo 3D Extensometer Link
    NodeType = '3D Extensometer Link';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNum3DEL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNum3DEL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNum3DEL ' '];
    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))];
    Date_Rif_3DEL = Data_num_ini(1);
    data1a = datenum(Date);
    data1b = Data_num_ini(1);
    if data1b>data1a
        data1 = data1b;
    else
        data1 = data1a;
    end
    
    if rLE==1 && cLE==1
        dataini3DEL = datestr(data1,'yyyy-mm-dd');
        tempoini3DEL = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZero3DEL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            dataini3DEL = datestr(data2,'yyyy-mm-dd');
            tempoini3DEL = datestr(data2,'HH:MM:SS');
        else
            dataini3DEL = datestr(data1,'yyyy-mm-dd');
            tempoini3DEL = datestr(data1,'HH:MM:SS');
        end
    end
    text = '3D Extensometer Link executed during LastElab function';
    fprintf(fileID,fmt,text);
    NuovoZero3DEL = 1;
else
    dataini3DEL = [];
    tempoini3DEL = [];
    Date_Rif_3DEL = [];
    NuovoZero3DEL = 0;
end

%% Wire Extensometer Link
if  yesWEL == 1 % Importo ultimo dato elaborato dei Wire Extensometer Link
    NodeNumWEL = num2str(cell2mat(NodoWireExtensometerLink(1,2))); % scarico i dati del primo nodo Wire Extensometer Link
    NodeType = 'Wire Extensometer Link';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNumWEL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumWEL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumWEL ' '];
    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
        datainiWEL = datestr(data1,'yyyy-mm-dd');
        tempoiniWEL = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZeroWEL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiWEL = datestr(data2,'yyyy-mm-dd');
            tempoiniWEL = datestr(data2,'HH:MM:SS');
        else
            datainiWEL = datestr(data1,'yyyy-mm-dd');
            tempoiniWEL = datestr(data1,'HH:MM:SS');
        end
        NuovoZeroWEL = 1;
    end
    text = 'Wire Extensometer Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiWEL = [];
    tempoiniWEL = [];
    NuovoZeroWEL = 0;
end

%% Multi Point Borehole Extensometer Link
if  yesMPBEL == 1 % Importo ultimo dato elaborato dei Multi Point Borehole Extensometer Link
    NodeNumMPBEL = num2str(cell2mat(NodoMultiPointRodExtensometer(1,2))); % scarico i dati del primo nodo MultiPoint Rod Extensometer
    NodeType = 'Multipoint borehole rod extensometer';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum = '  NodeNumMPBEL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 = '  NodeNumMPBEL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumMPBEL ' '];
    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
        datainiMPBEL = datestr(data1,'yyyy-mm-dd');
        tempoiniMPBEL = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZeroMPBEL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiMPBEL = datestr(data2,'yyyy-mm-dd');
            tempoiniMPBEL = datestr(data2,'HH:MM:SS');
        else
            datainiMPBEL = datestr(data1,'yyyy-mm-dd');
            tempoiniMPBEL = datestr(data1,'HH:MM:SS');
        end
    end
    NuovoZeroMPBEL = 1;
else
    datainiMPBEL = [];
    tempoiniMPBEL = [];
    NuovoZeroMPBEL = 0;
end

%% Analog Link
if  yesAL == 1 % Importo ultimo dato elaborato dei Analog Link
    AnalogCh = cell2mat(NodoAnalogLink(:,5));
    NodeNumAL = num2str(cell2mat(NodoAnalogLink(1,2))); % scarico i dati del primo nodo Analog Link
    NodeType = 'Analog Link';
    
     comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNumAL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumAL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumAL ' '];
    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
        datainiAL = datestr(data1,'yyyy-mm-dd');
        tempoiniAL = datestr(data1,'HH:MM:SS');
        NuovoZeroAL = 0;
        DatiElabAnalogLink = [];
        margine = 0;
        NuovoZeroAL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiAL = datestr(data2,'yyyy-mm-dd');
            tempoiniAL = datestr(data2,'HH:MM:SS');
            NuovoZeroAL = 1;
        else
            datainiAL = datestr(data1,'yyyy-mm-dd');
            tempoiniAL = datestr(data1,'HH:MM:SS');
            NuovoZeroAL = 0;
        end
    end
    
    if NuovoZeroAL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            NAnalogLink = cell2mat(NodoAnalogLink(:,2));
            ii = 1; % contatore
            n = 1;
            col = 2+AnalogCh(1,1); % contatore colonne
            while ii <= rAL
                ini_col = col-AnalogCh(n,1)-1;
                nN = num2str(NAnalogLink(ii,1));
                if AnalogCh == 1
                    comando = ['select EventDate, EventTime, XShift from ElabDataView where EventDate = ''' ...
                        Data_scarico ''' and  UnitName = ''' IDcentralina ''' and  ToolNameID = ''' DTcatena...
                        ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                elseif AnalogCh == 2
                    comando = ['select EventDate, EventTime, XShift, YShift from ElabDataView where EventDate = ''' ...
                        Data_scarico ''' and  UnitName = ''' IDcentralina ''' and  ToolNameID = ''' DTcatena...
                        ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                elseif AnalogCh == 3
                    comando = ['select EventDate, EventTime, XShift, YShift, ZShift from ElabDataView where EventDate = ''' ...
                        Data_scarico ''' and  UnitName = ''' IDcentralina ''' and  ToolNameID = ''' DTcatena...
                        ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                elseif AnalogCh == 4
                    comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X from ElabDataView where EventDate = ''' ...
                        Data_scarico ''' and  UnitName = ''' IDcentralina ''' and  ToolNameID = ''' DTcatena...
                        ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                elseif AnalogCh == 5
                  comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y from ElabDataView where EventDate = ''' ...
                        Data_scarico ''' and  UnitName = ''' IDcentralina ''' and  ToolNameID = ''' DTcatena...
                        ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                elseif AnalogCh == 6
                  comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z from ElabDataView where EventDate = ''' ...
                        Data_scarico ''' and  UnitName = ''' IDcentralina ''' and  ToolNameID = ''' DTcatena...
                        ''' and NodeType = ''' NodeType ''' and NodeNum = ''' nN ''' '];
                end
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoAL = curs.Data;
                DatiElabAnalogLink(:,ini_col:col) = DATnodoAL(:,:);
                ii = ii+1;
                if ii < rAL % non è l'ultimo sensore
                    if AnalogCh(n+1,1) == 1
                        col = col+3;
                    elseif AnalogCh(n+1,1) == 2
                        col = col+4;
                    elseif AnalogCh(n+1,1) == 3
                        col = col+5;
                    elseif AnalogCh(n+1,1) == 4
                        col = col+6;
                    elseif AnalogCh(n+1,1) == 5
                        col = col+7;
                    elseif AnalogCh(n+1,1) == 6
                        col = col+8;
                    end
                end
                
                if AnalogCh(n+1,1) == 1
                    Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,3:3:end)));
                elseif AnalogCh(n+1,1) == 2
                    Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,3:4:end)));
                elseif AnalogCh(n+1,1) == 3
                    Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,3:5:end)));
                elseif AnalogCh(n+1,1) == 4
                    Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,3:6:end)));
                elseif AnalogCh(n+1,1) == 5
                    Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,3:7:end)));
                elseif AnalogCh(n+1,1) == 6
                    Check = isnan(cell2mat(DatiElabTiltLinkHRH(1,3:8:end)));
                end
                
                for jj = 1:rAL
                    if Check == 1
                        datainiAL = datenum(datainiAL) - 1;
                        Data_scarico = datestr(datainiAL,'yyyy-mm-dd HH:MM:SS');
                        datainiAL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Analog Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabAnalogLink = [];
    end
 else
    datainiAL = [];
    tempoiniAL = [];
    DatiElabAnalogLink = [];
    NuovoZeroAL = 0;
end

%% Crack Link
if  yesCrL == 1 % Importo ultimo dato elaborato dei Crack Link
    NodeNumCrL = num2str(cell2mat(NodoCrackLink(1,2))); % scarico i dati del primo nodo Crack Link
    NodeType = 'Crack Link';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNumCrL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumCrL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumCrL ' '];
    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
        datainiCrL = datestr(data1,'yyyy-mm-dd');
        tempoiniCrL = datestr(data1,'HH:MM:SS');
        NuovoZeroCrL = 0;
        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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiCrL = datestr(data2,'yyyy-mm-dd');
            tempoiniCrL = datestr(data2,'HH:MM:SS');
        else
            datainiCrL = datestr(data1,'yyyy-mm-dd');
            tempoiniCrL = datestr(data1,'HH:MM:SS');
        end
        NuovoZeroCrL = 1;
    end
    text = 'Crack Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiCrL = [];
    tempoiniCrL = [];
    NuovoZeroCrL = 0;
end

%% 3D Crack Link
if  yes3DCrL == 1 % Importo ultimo dato elaborato dei 3D Crack Link
    NodeNum3DCrL = num2str(cell2mat(Nodo3DCrackLink(1,2))); % scarico i dati del primo nodo Crack Link
    NodeType = '3D Crack Link';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNum3DCrL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNum3DCrL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNum3DCrL ' '];
    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
        dataini3DCrL = datestr(data1,'yyyy-mm-dd');
        tempoini3DCrL = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZero3DCrL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            dataini3DCrL = datestr(data2,'yyyy-mm-dd');
            tempoini3DCrL = datestr(data2,'HH:MM:SS');
        else
            dataini3DCrL = datestr(data1,'yyyy-mm-dd');
            tempoini3DCrL = datestr(data1,'HH:MM:SS');
        end
        NuovoZero3DCrL = 1;
    end
    text = '3D Crack Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    dataini3DCrL = [];
    tempoini3DCrL = [];
    NuovoZero3DCrL = 0;
end

%% 2D Crack Link
if  yes2DCrL == 1 % Importo ultimo dato elaborato dei 2D Crack Link
    NodeNum2DCrL = num2str(cell2mat(Nodo2DCrackLink(1,2))); % scarico i dati del primo nodo 2D Crack Link
    NodeType = '2D Crack Link';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNum2DCrL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNum2DCrL ' '];
        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
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNum2DCrL ' '];
    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
        dataini2DCrL = datestr(data1,'yyyy-mm-dd');
        tempoini2DCrL = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZero2DCrL = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            dataini2DCrL = datestr(data2,'yyyy-mm-dd');
            tempoini2DCrL = datestr(data2,'HH:MM:SS');
        else
            dataini2DCrL = datestr(data1,'yyyy-mm-dd');
            tempoini2DCrL = datestr(data1,'HH:MM:SS');
        end
        NuovoZero2DCrL = 1;
    end
    text = '2D Crack Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    dataini2DCrL = [];
    tempoini2DCrL = [];
    NuovoZero2DCrL = 0;
end

%% Stress Meter
if  yesSM == 1 % Importo ultimo dato elaborato dei Stress Meter
    NodeNumSM = num2str(cell2mat(NodoStressMeter(1,2))); % scarico i dati del primo nodo
    NodeType = 'Stress Meter';
    
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina...
        ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' ...
        NodeType ''' and NodeNum ='  NodeNumSM ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && 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 ='  NodeNumSM ' '];
        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
    Date_Rif_SM = Data_num_ini(1);
    
    comando = ['select EventDate, EventTime from ElabDataView where EventDate >= ''' ...
        date ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
        ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumSM ' '];
    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
        datainiSM = datestr(data1,'yyyy-mm-dd');
        tempoiniSM = datestr(data1,'HH:MM:SS');
        margine = 0;
        NuovoZeroSM = 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; % margine
        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 < 6
            cont = 6;
        end
        [rDS,~] = size(Data_numero);
        if cont >= rDS
           cont = rDS-1; 
        end
        Data_scarico =  Data_numero(end-cont); % aggiornare contatori
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd HH:MM:SS');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiSM = datestr(data2,'yyyy-mm-dd');
            tempoiniSM = datestr(data2,'HH:MM:SS');
        else
            datainiSM = datestr(data1,'yyyy-mm-dd');
            tempoiniSM = datestr(data1,'HH:MM:SS');
        end
        NuovoZeroSM = 1;
    end
    text = 'Stress Meter executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiSM = [];
    tempoiniSM = [];
    Date_Rif_SM = [];
    NuovoZeroSM = 0;
end

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