function [NuovoZeroRSN,datainiRSN,tempoiniRSN,NuovoZeroRSNHR,datainiRSNHR,tempoiniRSNHR,...
    DatiElabTriggerLink,NuovoZeroTrL,datainiTrL,tempoiniTrL,DatiElabShockSensor,NuovoZeroSS,...
    datainiSS,tempoiniSS,NuovoZeroLL,datainiLL,tempoiniLL,datainiGF,tempoiniGF,datainiGS,tempoiniGS,...
    DatiElabDebrisLink,NuovoZeroDL,datainiDL,tempoiniDL]...
    = LastElab(conn,date,time,IDcentralina,DTcatena,NdatiMedia,NodoRSNLink,yesRSN,NodoRSNLinkHR,yesRSNHR,...
    NodoTriggerLink,yesTrL,rTrL,NodoSS,yesSS,rSS,NodoLoadLink,yesLL,yesGF,yesGS,NodoDebrisLink,yesDL,...
    rDL,Ndatidespike,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);

%% RSN Link
if  yesRSN == 1 % Importo ultimo dato elaborato dei RSN Link
    NodeNumRSN = num2str(cell2mat(NodoRSNLink(1,2))); % scarico i dati del primo nodo RSN Link
    NodeType = 'RSN 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 ='  NodeNumRSN ' '];
    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 ='  NodeNumRSN ' '];
        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 ='  NodeNumRSN ' '];
    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
        datainiRSN = datestr(data1,'yyyy-mm-dd');
        tempoiniRSN = datestr(data1,'HH:MM:SS');
        NuovoZeroRSN = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if margine < Ndati/2
            margine = round(Ndati/2+1);
        end
        cont = Ndati + margine;
        if cont < 3
            cont = 3;
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiRSN = datestr(data2,'yyyy-mm-dd');
            tempoiniRSN = datestr(data2,'HH:MM:SS');
            NuovoZeroRSN = 1; 
        else
            datainiRSN = datestr(data1,'yyyy-mm-dd');
            tempoiniRSN = datestr(data1,'HH:MM:SS');
            NuovoZeroRSN = 0;
        end
    end
    text = 'RSN Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiRSN = [];
    tempoiniRSN = [];
    NuovoZeroRSN = 0;
end

%% RSN Link HR 
if  yesRSNHR == 1 % Importo ultimo dato elaborato dei RSN Link HR
    NodeNumRSNHR = num2str(cell2mat(NodoRSNLinkHR(1,2))); % scarico i dati del primo nodo RSN Link HR
    NodeType = 'RSN Link HR';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and Time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumRSNHR ' '];
    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 ='  NodeNumRSNHR ' '];
        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 ='  NodeNumRSNHR ' '];
    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
        datainiRSNHR = datestr(data1,'yyyy-mm-dd');
        tempoiniRSNHR = datestr(data1,'HH:MM:SS');
        NuovoZeroRSNHR = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if margine < Ndati/2
            margine = round(Ndati/2+1);
        end
        cont = Ndati + margine;
        if cont < 3
            cont = 3;
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiRSNHR = datestr(data2,'yyyy-mm-dd');
            tempoiniRSNHR = datestr(data2,'HH:MM:SS');
            NuovoZeroRSNHR = 1; %1
        else
            datainiRSNHR = datestr(data1,'yyyy-mm-dd');
            tempoiniRSNHR = datestr(data1,'HH:MM:SS');
            NuovoZeroRSNHR = 0;
        end
    end
    text = 'RSN Link HR executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiRSNHR = [];
    tempoiniRSNHR = [];
    NuovoZeroRSNHR = 0;
end

%% Trigger Link
if  yesTrL == 1 % Importo ultimo dato elaborato dei Trigger Link
    NodeNumTrL = num2str(cell2mat(NodoTriggerLink(1,2))); % scarico i dati del primo nodo Trigger Link
    NodeType = 'Trigger 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 ='  NodeNumTrL ' '];
    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 ='  NodeNumTrL ' '];
        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 ='  NodeNumTrL ' '];
    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
        datainiTrL = datestr(data1,'yyyy-mm-dd');
        tempoiniTrL = datestr(data1,'HH:MM:SS');
        NuovoZeroTrL = 0;
        DatiElabTriggerLink = [];
    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;
        
        Data_scarico =  Data_numero(end)-margine;
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiTrL = datestr(data2,'yyyy-mm-dd');
            tempoiniTrL = datestr(data2,'HH:MM:SS');
            NuovoZeroTrL = 1;
        else
            datainiTrL = datestr(data1,'yyyy-mm-dd');
            tempoiniTrL = datestr(data1,'HH:MM:SS');
            NuovoZeroTrL = 0;
        end
    end
    
    if NuovoZeroTrL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiTrL) < datenum(date)
                datainiTrL = date;
            end
            NTriggerLink = cell2mat(NodoTriggerLink(:,2));
            ii = 1; % contatore
            col = 3; % contatore colonne
            Condition = 0;
            while ii <= rTrL
                ini_col = col-2;
                nN = num2str(NTriggerLink(ii,1));
                % Scarico di dati giorno X
                comando = ['select EventDate, EventTime, X 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);
                DATnodoTrLd = curs.Data;
                % Scarico di dati giorni successivi
                comando = ['select EventDate, EventTime, X 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);
                 DATnodoTrL = curs.Data;
                 [~,c1] = size(DATnodoTrL);
                 [~,c2] = size(DATnodoTrLd);
                 if c1 == c2
                     DATnodoTrL = [DATnodoTrLd; DATnodoTrL];
                 elseif c1 < c2 % solo giorno di riferimento
                     DATnodoTrL = DATnodoTrLd;
                 end
                [rDAT,cDAT] = size(DATnodoTrL);
                if rDAT==1 && cDAT==1
                    clear DatiElabTriggerLink
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,conn,FileName);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabTriggerLink);
                       if rDE ~= rDAT
                          clear DatiElabTriggerLink
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,conn,FileName);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabTriggerLink(:,ini_col:col) = DATnodoTrL(:,:);
                end
                ii = ii+1;
            end
            if Condition == 1
               datainiTrL = datenum(datainiTrL) - 1;
               Data_scarico = datestr(datainiTrL,'yyyy-mm-dd HH:MM:SS');
               datainiTrL = Data_scarico;
               fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabTriggerLink(1,3:16:end)));
                for jj = 1:rTrL
                    if Check == 1
                        datainiTrL = datenum(datainiTrL) - 1;
                        Data_scarico = datestr(datainiTrL,'yyyy-mm-dd HH:MM:SS');
                        datainiTrL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Trigger Link executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabTriggerLink = [];
    end
else
    datainiTrL = [];
    tempoiniTrL = [];
    DatiElabTriggerLink = [];
    NuovoZeroTrL = 0;
end

%% Shock Sensor
if yesSS == 1 % Importo ultimo dato elaborato 
    NodeNumSS = num2str(cell2mat(NodoSS(1,2))); % scarico i dati del primo nodo
    NodeType = 'Shock Sensor';
    
    % 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 ='  NodeNumSS ' '];
    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 ='  NodeNumSS ' '];
        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 ='  NodeNumSS ' '];
    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
        datainiSS = datestr(data1,'yyyy-mm-dd');
        tempoiniSS = datestr(data1,'HH:MM:SS');
        NuovoZeroSS = 0;
        DatiElabShockSensor = [];
    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;
        
        Data_scarico =  Data_numero(end)-margine;
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiSS = datestr(data2,'yyyy-mm-dd');
            tempoiniSS = datestr(data2,'HH:MM:SS');
            NuovoZeroSS = 1;
        else
            datainiSS = datestr(data1,'yyyy-mm-dd');
            tempoiniSS = datestr(data1,'HH:MM:SS');
            NuovoZeroSS = 0;
        end
    end
    
    if NuovoZeroSS == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiSS) < datenum(date)
                datainiSS = date;
            end
            NShockSensor = cell2mat(NodoSS(:,2));
            ii = 1; % contatore
            col = 3; % contatore colonne
            Condition = 0;
            while ii <= rSS
                ini_col = col-2;
                nN = num2str(NShockSensor(ii,1));
                % Scarico di dati giorno X
                comando = ['select EventDate, EventTime, X 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);
                DATnodoSSd = curs.Data;
                % Scarico di dati giorni successivi
                comando = ['select EventDate, EventTime, X 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);
                 DATnodoSS = curs.Data;
                 [~,c1] = size(DATnodoSS);
                 [~,c2] = size(DATnodoSSd);
                 if c1 == c2
                     DATnodoSS = [DATnodoSSd; DATnodoSS];
                 elseif c1 < c2 % solo giorno di riferimento
                     DATnodoSS = DATnodoSSd;
                 end
                [rDAT,cDAT] = size(DATnodoSS);
                if rDAT==1 && cDAT==1
                    clear DatiElabShockSensor
                    fclose(fileID);
                    cancellaDB(IDcentralina,DTcatena,Data_scarico,conn,FileName);
                    fileID = fopen(FileName,'a');
                    fmt = '%s \r';
                    Condition = 1;
                    break
                else
                    if ii > 1 % Nodo successivo al primo
                       [rDE,~] = size(DatiElabShockSensor);
                       if rDE ~= rDAT
                          clear DatiElabShockSensor
                          fclose(fileID);
                          cancellaDB(IDcentralina,DTcatena,Data_scarico,conn,FileName);
                          fileID = fopen(FileName,'a');
                          fmt = '%s \r';
                          Condition = 1;
                          break
                       end
                    end
                    DatiElabShockSensor(:,ini_col:col) = DATnodoSS(:,:);
                end
                ii = ii+1;
            end
            if Condition == 1
               datainiSS = datenum(datainiSS) - 1;
               Data_scarico = datestr(datainiSS,'yyyy-mm-dd HH:MM:SS');
               datainiSS = Data_scarico;
               fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabShockSensor(1,3:16:end)));
                for jj = 1:rSS
                    if Check == 1
                        datainiSS = datenum(datainiSS) - 1;
                        Data_scarico = datestr(datainiSS,'yyyy-mm-dd HH:MM:SS');
                        datainiSS = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
        text = 'Shock Sensor executed during LastElab function';
        fprintf(fileID,fmt,text);
    else
        DatiElabShockSensor = [];
    end
else
    datainiSS = [];
    tempoiniSS = [];
    DatiElabShockSensor = [];
    NuovoZeroSS = 0;
end

%% Load Link
if  yesLL == 1 % Importo ultimo dato elaborato dei Load Link
    NodeNumLL = num2str(cell2mat(NodoLoadLink(1,2))); % scarico i dati del primo nodo Load Link
    NodeType = 'Load Link';
    
    % Dati Grezzi
    comando = ['select Date, Time from RawDataView where Date = ''' ...
        date ''' and time >= ''' time ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeType = ''' NodeType ''' and NodeNum ='  NodeNumLL ' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 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');
        NuovoZeroLL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;

        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if margine < Ndati/2
            margine = round(Ndati/2+1);
        end
        cont = Ndati + margine;
        if cont < 3
            cont = 3;
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiLL = datestr(data2,'yyyy-mm-dd');
            tempoiniLL = datestr(data2,'HH:MM:SS');
            NuovoZeroLL = 1; %1
        else
            datainiLL = datestr(data1,'yyyy-mm-dd');
            tempoiniLL = datestr(data1,'HH:MM:SS');
            NuovoZeroLL = 0;
        end
    end
    text = 'Load Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiLL = [];
    tempoiniLL = [];
    NuovoZeroLL = 0;
end

%% G-Flow Link
if  yesGF == 1 % Importo ultimo dato elaborato G-Flow
  
    % Dati Grezzi
    DATA = [date time];
    comando = ['select EventDateTime from ELABDATAGFLOW where EventDateTime >= ''' ...
        DATA ''' and unit_name = ''' IDcentralina ''' and tool_name = '''...
        DTcatena ''' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = D_num_ini;
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    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 rI==1 && cI==1
        datainiGF = datestr(data1,'yyyy-mm-dd');
        tempoiniGF = datestr(data1,'HH:MM:SS');
    else    
        % Modifico il formato di data e ora in DATini.
        Data_numero = Data_num_ini(end);
        margine = 15;
        
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if margine < Ndati/2
            margine = round(Ndati/2+1);
        end
        cont = Ndati + margine;
        if cont < 2
            cont = 2;
        end
        Data_scarico =  Data_numero(end)-cont;
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiGF = datestr(data2,'yyyy-mm-dd');
            tempoiniGF = datestr(data2,'HH:MM:SS');
        else
            datainiGF = datestr(data1,'yyyy-mm-dd');
            tempoiniGF = datestr(data1,'HH:MM:SS');
        end
    end
    text = 'G-Flow Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiGF = [];
    tempoiniGF = [];
end

%% G-Shock Link
if  yesGS == 1 % Importo ultimo dato elaborato dei RSN Link
   
    % Dati Grezzi
    DATA = [date time];
    comando = ['select EventDateTime from ELABDATAGFLOW where EventDateTime >= ''' ...
        DATA ''' and unit_name = ''' IDcentralina ''' and tool_name = '''...
        DTcatena ''' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    D_num_ini = curs.Data;
    [rI,cI] = size(D_num_ini);
    if rI == 1 && cI == 1
        Data_num_ini = 0;
    else
        Data_num_ini = D_num_ini;
    end
    Data_num_ini = datenum(Data_num_ini); % tutte le date presenti nel DB grezzo
    
    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 rI==1 && cI==1
        datainiGS = datestr(data1,'yyyy-mm-dd');
        tempoiniGS = datestr(data1,'HH:MM:SS');
    else    
        % Modifico il formato di data e ora in DATini.
        Data_numero = Data_num_ini(end);
        margine = 15;
        
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if margine < Ndati/2
            margine = round(Ndati/2+1);
        end
        cont = Ndati + margine;
        if cont < 2
            cont = 2;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiGS = datestr(data2,'yyyy-mm-dd');
            tempoiniGS = datestr(data2,'HH:MM:SS');
        else
            datainiGS = datestr(data1,'yyyy-mm-dd');
            tempoiniGS = datestr(data1,'HH:MM:SS');
        end
    end
    text = 'G-Shock Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiGS = [];
    tempoiniGS = [];
end

%% Debris Link
if  yesDL == 1 % Importo ultimo dato elaborato dei RSN Link
    NodeNumDL = num2str(cell2mat(NodoDebrisLink(1,2))); % scarico i dati del primo nodo
    NodeType = 'Debris 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 ='  NodeNumDL ' '];
    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 ='  NodeNumDL ' '];
        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 ='  NodeNumDL ' '];
    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
        datainiDL = datestr(data1,'yyyy-mm-dd');
        tempoiniDL = datestr(data1,'HH:MM:SS');
        NuovoZeroDL = 0;
    else    
        % Modifico il formato di data e ora in DATini.
        [rD,~] = size(Data_LastElab);
        Data_numero = [cell2mat(Data_LastElab(:,1)) repmat(' ', [rD,1]) cell2mat(Data_LastElab(:,2))];
        Data_numero = datenum(Data_numero); % tutte le date presenti nel DB grezzo
        margine = 1;
        
        if Ndatidespike > NdatiMedia
            Ndati = Ndatidespike;
        else
            Ndati = NdatiMedia;
        end
        if margine < Ndati/2
            margine = round(Ndati/2+1);
        end
        cont = Ndati + margine;
        if cont < 3
            cont = 3;
        end
        [rDS,~] = size(Data_numero);
        if rDS <= cont
            cont = rDS-1;
        end
        Data_scarico =  Data_numero(end-cont);
        Data_scarico = datestr(Data_scarico,'yyyy-mm-dd');
        data2 = datenum(Data_scarico);
        if data2 > data1 
            datainiDL = datestr(data2,'yyyy-mm-dd');
            tempoiniDL = datestr(data2,'HH:MM:SS');
            NuovoZeroDL = 1; 
        else
            datainiDL = datestr(data1,'yyyy-mm-dd');
            tempoiniDL = datestr(data1,'HH:MM:SS');
            NuovoZeroDL = 0;
        end
    end
    
    if NuovoZeroDL == 1 % scarico gli ultimi dati elaborati
        NAN = 0;
        while NAN == 0
            if datenum(datainiDL) < datenum(date)
                datainiDL = date;
            end
            NDebrisLink = cell2mat(NodoDebrisLink(:,2));
            ii = 1; % contatore
            col = 11; % contatore colonne
            Condition = 0;
            while ii <= rDL
                ini_col = col-10;
                nN = num2str(NDebrisLink(ii,1));
                % Scarico di dati di spostamenti
                comando = ['select EventDate, EventTime, XShift, YShift, ZShift, X, Y, Z, T_node, 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, YShift, ZShift, X, Y, Z, T_node, 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);
                DATnodoDL = curs.Data;
                [~,c1] = size(DATnodoDL);
                [~,c2] = size(DATnodoDLd);
                if c1 == c2
                    DATnodoDL = [DATnodoDLd; DATnodoDL];
                elseif c1 < c2 % solo giorno di riferimento
                    DATnodoDL = DATnodoDLd;
                end
                [rDAT,cDAT] = size(DATnodoDL);
                if rDAT==1 && cDAT==1
                    clear DatiElabDebrisLink
                    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(DatiElabDebrisLink);
                        if rDE ~= rDAT
                            clear DatiElabDebrisLink
                            fclose(fileID);
                            cancellaDB(IDcentralina,DTcatena,Data_scarico,FileName,conn);
                            fileID = fopen(FileName,'a');
                            fmt = '%s \r';
                            Condition = 1;
                            break
                        end
                    end
                    DatiElabDebrisLink(:,ini_col:col) = DATnodoDL(:,:);
                end
                col = col+11;
                ii = ii+1;
            end
            
            if Condition == 1
                datainiDL = datenum(datainiDL) - 1;
                Data_scarico = datestr(datainiDL,'yyyy-mm-dd HH:MM:SS');
                datainiDL = Data_scarico;
                fileID = fopen(FileName,'a');
            else
                Check = isnan(cell2mat(DatiElabDebrisLink(1,3:16:end)));
                for jj = 1:rTuL
                    if Check == 1
                        datainiDL = datenum(datainiDL) - 1;
                        Data_scarico = datestr(datainiDL,'yyyy-mm-dd HH:MM:SS');
                        datainiDL = Data_scarico;
                        break
                    else
                        NAN = 1;
                    end
                end
            end
        end
    end
    text = 'Debris Link executed during LastElab function';
    fprintf(fileID,fmt,text);
else
    datainiDL = [];
    tempoiniDL = [];
    NuovoZeroDL = 0;
    DatiElabDebrisLink = [];
end

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