function [GI_PCL_Z,GI_PCL_Date,ATTIVA,FIG,FIG_ENG] = report_PCL(DTcatena,rPCL,m,NodoPreConvLink,...
    toolrif,unitrif,datarif,firstdata_num,Font_caption,Font_tools,siteID,DT,DT_ENG,FIG,FIG_ENG,br,...
    colonna2,colonna2bis,status,ATTIVA,activeEN,conn,FileName)

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

import mlreportgen.dom.*
import mlreportgen.report.*

% Make sure DOM is compilable
makeDOMCompilable()

check = 0;
while check == 0
    if status(m,1) == 1
        if ATTIVA == 1
            ATTIVA = 0;
            testo = Paragraph('Catena non ancora installata o inattiva.');
            add(DT,testo);
            if activeEN == 1
                testo = Paragraph('Array not installed or inactive.');
                add(DT_ENG,testo);
            end
        end
        check = 1;
    elseif status(m,1) == 3
        if ATTIVA == 1
            ATTIVA = 0;
            testo = Paragraph(['Monitoraggio completato. Invitiamo a visitare la '...
                'piattaforma web per la visione dei dati storici.']);
            add(DT,testo);
            if activeEN == 1
                testo = Paragraph(['Monitoring completed. Please log into the web-based platoform '...
                    'to access the complete dataset.']);
                add(DT_ENG,testo);
            end
        end
        check = 1;
    else
        NODATAPCL(m,1) = 0;
        date = datestr(datenum(colonna2{m+1,1},'dd-mm-yyyy'),'yyyy-mm-dd'); % data riferimento definita sul sito
        % posizione di progetto dei nodi
        comando = ['select EventDate, EventTime, Y, Z from ElabDataView where EventDate = ''' ...
            date ''' and  ToolNameID = ''' char(toolrif) ''' and UnitName = ''' char(unitrif) ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        SezioneYZ = curs.Data;
        Datasez = SezioneYZ(1,1);
        Orasez = SezioneYZ(1,2);
        [ry,~] = size(SezioneYZ);
        scrivi = 1;
        % ho scaricato tutti i dati del giorno di installazione, prendo
        % i primi in ordine temporale per ciascun nodo
        for cc = 1:ry
            if strcmp(SezioneYZ(cc,1),Datasez)==1 && strcmp(SezioneYZ(cc,2),Orasez) ==1
                CoordY(scrivi,1) = SezioneYZ(cc,3);
                CoordZ(scrivi,1) = SezioneYZ(cc,4);
                scrivi = scrivi+1;
            end
        end
        
        comando = ['select id from units where name like ''' char(unitrif) ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        unitID = curs.Data;
        
        % Data di fine PreConvergenza
        comando = ['select prod_date_node from tools where unit_ID like ''' ...
            num2str(cell2mat(unitID)) ''' and name like ''' DTcatena ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        dati = curs.Data;
        stringa = cell2mat(dati(1,1));
        if strcmp(stringa,'null') || strcmp(stringa,'No Data')
            data_PC = 0;
        else
            data_PC = stringa(1,1:10);
        end
        if data_PC ~= 0
            % Scarico dati di Fine PreConvergenza
            Num = zeros(rPCL(m,1),1);
            for p = 1:rPCL(m,1)
                NodeNum = num2str(NodoPreConvLink(p,m));
                comando = ['select EventDate, EventTime, ZShift, Z, Y from ElabDataView where EventDate < ''' ...
                    data_PC ''' and  ToolNameID = ''' char(toolrif) ''' and UnitName = ''' char(unitrif) ...
                    ''' and NodeNum = ''' NodeNum ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                Dati_PC = curs.Data;
                
                Num(p,1) = NodoPreConvLink(p,m);
                
                [rD,rC] = size(Dati_PC);
                if rC ~= 1
                    T = [cell2mat(Dati_PC(:,1)) repmat(' ', [rD,1]) cell2mat(Dati_PC(:,2))];
                    Date = datenum(T); % Data
                    
                    % Fine Pre-Convergenza
                    asseZ_PC(:,p) = 1000*cell2mat(Dati_PC(:,3));
                    asseZ_Cum_PC(:,p) = 1000*cell2mat(Dati_PC(:,4));
                    diffZ_PC(:,p) = asseZ_PC(:,p)-asseZ_PC(1,p); % mm
                    Z_Loc_PC(1,p) = asseZ_PC(end,p)-asseZ_PC(1,p);
                    Z_Cum_PC(1,p) = asseZ_Cum_PC(end,p)-asseZ_Cum_PC(1,p);
                    Y_PC(1,p) = cell2mat(Dati_PC(end,5));
                end
            end
        end
        
        % Scarico dati odierni
        Num = zeros(rPCL(m,1),1);
        for p = 1:rPCL(m,1)
            NodeNum = num2str(NodoPreConvLink(p,m));
            comando = ['select EventDate, EventTime, ZShift, Z, Y from ElabDataView where EventDate >= ''' ...
                datarif ''' and  ToolNameID = ''' char(toolrif) ''' and UnitName = ''' char(unitrif) ...
                ''' and NodeNum = ''' NodeNum ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            Dati = curs.Data;
            Num(p,1) = NodoPreConvLink(p,m);
            
            [rD,rC] = size(Dati);
            if rC ~= 1
                T = [cell2mat(Dati(:,1)) repmat(' ', [rD,1]) cell2mat(Dati(:,2))];
                Date = datenum(T); % Data
                if p == 1 % Il primo lo scrivo e creo la variabile di controllo
                else
                    [rS,~] = size(asseZ);
                    if rD ~= rS
                        NODATA(m,1) = 1;
                        check = 0;
                        break
                    else
                        check = 1;
                    end
                end
                
                % Ultimo mese
                asseZ(:,p) = 1000*cell2mat(Dati(:,3));
                asseZ_Cum(:,p) = 1000*cell2mat(Dati(:,4));
                diffZ(:,p) = asseZ(:,p)-asseZ(1,p); % mm
                Z_Loc(1,p) = asseZ(end,p)-asseZ(1,p);
                Z_Cum(1,p) = asseZ_Cum(end,p)-asseZ_Cum(1,p);
                Z_Loc_Tot(1,p) = asseZ(end,p);
                Z_Cum_Tot(1,p) = asseZ_Cum(end,p);
                Y(1,p) = cell2mat(Dati(end,5));
                
            else
                NODATAPCL(m,1) = 1;
                check = 1;
            end
        end
        
        if NODATAPCL(m,1) == 0
            if strcmp(colonna2bis(m+1,1),colonna2(m+1,1)) == 0 % data zero diversa data riferimento
                datarifNEW = datestr(firstdata_num{m+1,1},'yyyy-mm-dd'); % data riferimento definita sul sito
                for nw = 1:rPCL(m,1)
                    NodeNum = num2str(NodoPreConvLink(nw,m));
                    comando = ['select EventDate, EventTime, ZShift, Z, Y from ElabDataView where EventDate >= ''' ...
                        datarifNEW ''' and  ToolNameID = ''' char(toolrif) ''' and UnitName = ''' char(unitrif) ...
                        ''' and NodeNum = ''' NodeNum ''' '];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    DatiNEW = curs.Data;
                    Num(nw,1) = NodoPreConvLink(nw,m);
                    
                    [~,rC] = size(DatiNEW);
                    if rC ~= 1
                        % dalla data di riferimento
                        % divergenza/convergenza dei singoli nodi dall'inizio
                        Z_Loc_Tot(1,nw) = asseZ(end,nw)-1000*cell2mat(DatiNEW(1,3));
                        Z_Cum_Tot(1,nw) = asseZ_Cum(end,nw)-1000*cell2mat(DatiNEW(1,4));
                        if data_PC ~= 0
                            % Solo periodo di PreConvergenza
                            Z_Loc_PC(1,nw) = asseZ_PC(end,nw)-1000*cell2mat(DatiNEW(1,3));
                            Z_Cum_PC(1,nw) = asseZ_Cum_PC(end,nw)-1000*cell2mat(DatiNEW(1,4));
                        end
                    else
                        NODATAPCL(m,1) = 1;
                    end
                end
            end
            GI_PCL_Date = Date;
            % Cerco eventuali correlazioni con Cir Array
            NomeFile = strcat('Report',siteID,'-PCLvsTuL.txt');
            Filetesto = importdata(NomeFile);
            [rF,~] = size(Filetesto);
            AAA = 1;
            Var = 0;
            while AAA < rF % A un PreConv possono competere più Cir
                check1 = Filetesto(AAA,1);
                if check1 == 99999 % Non ci sono PreConv Array o non ci sono Cir
                    AAA = 2;
                else
                    check2 = Filetesto(AAA+1,1);
                    if check1 == str2num(DTcatena(end-2:end)) % Il PreConv è da prendere in considerazione ed è riportato nella prima riga del file
                        Nodo = Filetesto(AAA+2,1);
                        Var = 1;
                    elseif check2 == str2num(DTcatena(end-2:end)) % Il PreConv è da prendere in considerazione ed è riportato nella seconda riga del file
                        Nodo = Filetesto(AAA+3,1);
                        Var = 1;
                    end
                    AAA = AAA+4;
                end
            end
            if Var == 1
                GI_PCL_Z = diffZ(:,Nodo);
            else
                GI_PCL_Z = [];
            end
            
            % --- grafico spostamenti locali - Periodi di PreConvergenza ---
            figure(1)
            Y = round(Y);
            if data_PC ~= 0
                Disp_Loc_PC = bar(Y_PC,Z_Loc_PC');
                Z = Z_Loc_PC;
            else
                Disp_Loc_PC = bar(Y,Z_Loc');
                Z = Z_Loc;
            end
            title('Spostamenti locali - Periodo di Pre-Convergenza');
            ylabel('Spostamento [mm]');
            xlabel('Progressiva [m]');
            maxY = max(Y);
            if maxY > 0
                xticks(0:1:maxY)
            else
                xticks(Y(end):1:0);
            end
            h = gca;
            h.YAxis.MinorTick = 'on';
            grid on
            if maxY < 0 % mm
                xlim([Y_PC(end)-1 0]);
            else
                xlim([0 maxY+1])
            end
            grid on
            iniY = min(min(Z',Z_Loc'));
            endY = max(max(Z',Z_Loc'));
            MAX = max(abs(iniY),endY);
            if MAX > 2.5 % Limiti Asse Y
                ylim([-MAX-1 MAX+1])
            else
                ylim([-2.5 +2.5]);
            end
            TempName1 = char(strcat('Report',siteID,'-', char(toolrif),'Z_Loc_PC_PCL.png'));
            saveas(Disp_Loc_PC(1),TempName1);
            close(figure(1))
            
            % ENG
            figure(1)
            if data_PC ~= 0
                Disp_Loc_PC = bar(Y_PC,Z_Loc_PC');
            else
                Disp_Loc_PC = bar(Y,Z_Loc');
            end
            title('Local displacements - Pre-Convergence phase');
            ylabel('Displacement [mm]');
            xlabel('Position [m]');
            if maxY > 0
                xticks(0:1:maxY)
            else
                xticks(Y(end):1:0);
            end
            h = gca;
            h.YAxis.MinorTick = 'on';
            grid on
            if maxY < 0 % mm
                xlim([Y_PC(end)-1 0]);
            else
                xlim([0 maxY+1])
            end
            grid on
            if MAX > 2.5 % Limiti Asse Y
                ylim([-MAX-1 MAX+1])
            else
                ylim([-2.5 +2.5]);
            end
            TempName1_ENG = char(strcat('Report',siteID,'-', char(toolrif),'Z_Loc_PC_PCL_ENG.png'));
            saveas(Disp_Loc_PC(1),TempName1_ENG);
            close(figure(1))
            
            % --- grafico spostamenti locali - Periodi di riferimento ---
            figure(2)
            Y = round(Y);
            Disp_Loc_PCL = bar(Y,Z_Loc');
            title('Spostamenti locali - Periodo di riferimento');
            ylabel('Spostamento [mm]');
            xlabel('Progressiva [m]');
            maxY = max(Y);
            minY = min(Y);
            MAXY = max(abs(minY),maxY);
            IY = find(max(abs(minY),maxY) == MAXY);
            maxY = Y(IY);
            if maxY > 0
                xticks(0:1:maxY)
            else
                xticks(Y(end):1:0);
            end
            h = gca;
            h.YAxis.MinorTick = 'on';
            grid on
            if MAX > 2.5 % Limiti Asse Y
                ylim([-MAX-1 MAX+1])
            else
                ylim([-2.5 +2.5]);
            end
            grid on
            if iniY < 2.5 && endY > 2.5 % Limiti Asse Y
            elseif iniY < 2.5
                ylim([iniY-1 2.5]);
            elseif endY < 2.5
                ylim([-2.5 endY+1]);
            end
            TempName2 = char(strcat('Report',siteID,'-', char(toolrif),'Z_Loc_PCL.png'));
            saveas(Disp_Loc_PCL(1),TempName2);
            close(figure(2))
            
            ChartPCL = Image(TempName1);
            ChartPCL2 = Image(TempName2);
            lot1 = Table({ChartPCL, ChartPCL2});
            lot1.Style = {ResizeToFitContents(false), Width('100%')};
            ChartPCL.Style = {Height('6.6cm'),HAlign('center')};
            ChartPCL2.Style = {Height('6.6cm'),HAlign('center')};
            ChartPCL_cap = Paragraph(['Fig. ' num2str(FIG) ' - Spostamenti '...
                'differenziali locali in direzione verticale registrati durante '...
                'il periodo di pre-convergenza e il periodo di riferimento']);
            ChartPCL_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
            FIG = FIG+1;
            
            if activeEN == 1
                figure(2)
                Y = round(Y);
                Disp_Loc_PCL = bar(Y,Z_Loc');
                title('Local displacements - Reference time period');
                ylabel('Displacement [mm]');
                xlabel('Position [m]');
                maxY = Y(IY);
                if maxY > 0
                    xticks(0:1:maxY)
                else
                    xticks(Y(end):1:0);
                end
                h = gca;
                h.YAxis.MinorTick = 'on';
                grid on
                if MAX > 2.5 % Limiti Asse Y
                    ylim([-MAX-1 MAX+1])
                else
                    ylim([-2.5 +2.5]);
                end
                grid on
                if iniY < 2.5 && endY > 2.5 % Limiti Asse Y
                elseif iniY < 2.5
                    ylim([iniY-1 2.5]);
                elseif endY < 2.5
                    ylim([-2.5 endY+1]);
                end
                TempName2_ENG = char(strcat('Report',siteID,'-', char(toolrif),'Z_Loc_PCL_ENG.png'));
                saveas(Disp_Loc_PCL(1),TempName2_ENG);
                close(figure(2))
                
                ChartPCL_ENG = Image(TempName1_ENG);
                ChartPCL2_ENG = Image(TempName2_ENG);
                lot1_ENG = Table({ChartPCL_ENG, ChartPCL2_ENG});
                lot1_ENG.Style = {ResizeToFitContents(false), Width('100%')};
                ChartPCL_ENG.Style = {Height('6.6cm'),HAlign('center')};
                ChartPCL2_ENG.Style = {Height('6.6cm'),HAlign('center')};
                ChartPCL_ENG_cap = Paragraph(['Fig. ' num2str(FIG_ENG) ' - Local differential displacements '...
                    'recorded in vertical direction during the pre-convergence phase and the '...
                    'reference time period']);
                ChartPCL_ENG_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
                FIG_ENG = FIG_ENG+1;
            end
            
            % ---Grafico Sezione Periodo PreConvergenza---
            figure(3)
            contaS = 1;
            contaG = 1;
            contaF = 1;
            for disp = 1:rPCL(m,1)
                if diffZ_PC(end,disp)>0.5
                    nodisuZ(contaS,1)=CoordZ(disp,1);
                    nodisuY(contaS,1)=CoordY(disp,1);
                    contaS = contaS + 1;
                elseif diffZ_PC(end,disp)< -0.5
                    nodigiuZ(contaG,1)=CoordZ(disp,1);
                    nodigiuY(contaG,1)=CoordY(disp,1);
                    contaG = contaG + 1;
                else
                    nodistabZ(contaF,1)=CoordZ(disp,1);
                    nodistabY(contaF,1)=CoordY(disp,1);
                    contaF = contaF + 1;
                end
            end
            %aggiungo coordinate ancora
            if cell2mat(CoordY(1,1)) > cell2mat(CoordY(end,1))
                plot([cell2mat(CoordY(:,1)); 0],[cell2mat(CoordZ(:,1)); 0],'-','Color','black','MarkerSize' ,7);
            else
                plot([0; cell2mat(CoordY(:,1))],[0; cell2mat(CoordZ(:,1))],'-','Color','black','MarkerSize' ,7)
            end
            hold on
            if contaS > 1
                sezS = plot(cell2mat(nodisuY(:,1)),cell2mat(nodisuZ(:,1)),'^','Color','red','MarkerSize' ,7);
                set(sezS, 'markerfacecolor', get(sezS, 'color'));
            end
            if contaG > 1
                sezG = plot(cell2mat(nodigiuY(:,1)),cell2mat(nodigiuZ(:,1)),'v','Color',[0.4660 0.6740 0.1880],'MarkerSize' ,7);
                set(sezG, 'markerfacecolor', get(sezG, 'color'));
            end
            if contaF > 1
                sezN = plot(cell2mat(nodistabY(:,1)),cell2mat(nodistabZ(:,1)),'o','Color','black');
                set(sezN, 'markerfacecolor', get(sezN, 'color'));
            end
            ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
            set(ancora, 'markerfacecolor', get(ancora, 'color'));
            if contaS > 1 && contaG > 1 && contaF > 1
                legend('Geometria dello strumento','Innalzamento','Abbassamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS > 1 && contaG > 1 && contaF == 1
                legend('Geometria dello strumento','Innalzamento','Abbassamento','Ancora','Location','southoutside');
            elseif contaS > 1 && contaG == 1 && contaF > 1
                legend('Geometria dello strumento','Innalzamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS == 1 && contaG > 1 && contaF > 1
                legend('Geometria dello strumento','Abbassamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS > 1 && contaG == 1 && contaF == 1
                legend('Geometria dello strumento','Innalzamento','Ancora','Location','southoutside');
            elseif contaS == 1 && contaG > 1 && contaF == 1
                legend('Geometria dello strumento','Abbassamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS == 1 && contaG == 1 && contaF > 1
                legend('Geometria dello strumento','Nodi stabili','Ancora','Location','southoutside');
            end
            title('Direzione degli spostamenti - periodo di pre-convergenza');
            xlabel('Progressiva [m]');
            ylabel('Direzione verticale');
            set(gca,'YTickLabel',[]);
            set(gca,'YTick',[]);
            ini = min([0; cell2mat(CoordY)]);
            End = max([0; cell2mat(CoordY)]);
            h = gca;
            if ini >= -15 || End <= 15
                if ini == 0
                    xticks(ini:1:End+1);
                    xlim([ini,End+1]);
                elseif End == 0
                    xticks(ini-1:1:End);
                    xlim([ini-1,End]);
                end
            else
                h.XAxis.MinorTick = 'on';
            end
            grid off
            axes('pos',[0.1 0.8 .3 .1])
            imshow('direzionescavo.png')
            hold off
            TempName = char(strcat('Report',siteID,'-', char(toolrif),'SezPCL.png'));
            saveas(ancora,TempName);
            ChartSEZPC1 = Image(TempName);
            ChartSEZPC1.Style = {Height('6.6cm')};
            close(figure(3))
            
            % ENG
            if activeEN == 1
                % ---Grafico Sezione Periodo PreConvergenza---
                figure(3)
                %aggiungo coordinate ancora
                if cell2mat(CoordY(1,1)) > cell2mat(CoordY(end,1))
                    plot([cell2mat(CoordY(:,1)); 0],[cell2mat(CoordZ(:,1)); 0],'-','Color','black','MarkerSize' ,7);
                else
                    plot([0; cell2mat(CoordY(:,1))],[0; cell2mat(CoordZ(:,1))],'-','Color','black','MarkerSize' ,7)
                end
                hold on
                if contaS > 1
                    sezS = plot(cell2mat(nodisuY(:,1)),cell2mat(nodisuZ(:,1)),'^','Color','red','MarkerSize' ,7);
                    set(sezS, 'markerfacecolor', get(sezS, 'color'));
                end
                if contaG > 1
                    sezG = plot(cell2mat(nodigiuY(:,1)),cell2mat(nodigiuZ(:,1)),'v','Color',[0.4660 0.6740 0.1880],'MarkerSize' ,7);
                    set(sezG, 'markerfacecolor', get(sezG, 'color'));
                end
                if contaF > 1
                    sezN = plot(cell2mat(nodistabY(:,1)),cell2mat(nodistabZ(:,1)),'o','Color','black');
                    set(sezN, 'markerfacecolor', get(sezN, 'color'));
                end
                ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
                set(ancora, 'markerfacecolor', get(ancora, 'color'));
                if contaS > 1 && contaG > 1 && contaF > 1
                    legend('Array geometry','Upwards','Downwards','Stable node','Anchor','Location','southoutside');
                elseif contaS > 1 && contaG > 1 && contaF == 1
                    legend('Array geometry','Upwards','Downwards','Anchor','Location','southoutside');
                elseif contaS > 1 && contaG == 1 && contaF > 1
                    legend('Array geometry','Upwards','Stable node','Anchor','Location','southoutside');
                elseif contaS == 1 && contaG > 1 && contaF > 1
                    legend('Array geometry','Downwards','Stable node','Anchor','Location','southoutside');
                elseif contaS > 1 && contaG == 1 && contaF == 1
                    legend('Array geometry','Upwards','Anchor','Location','southoutside');
                elseif contaS == 1 && contaG > 1 && contaF == 1
                    legend('Array geometry','Downwards','Stable node','Anchor','Location','southoutside');
                elseif contaS == 1 && contaG == 1 && contaF > 1
                    legend('Array geometry','Stable node','Anchor','Location','southoutside');
                end
                title('Displacement direction - Pre-convergence phase');
                xlabel('Position [m]');
                ylabel('Vertical direction');
                set(gca,'YTickLabel',[]);
                set(gca,'YTick',[]);
                h = gca;
                if ini >= -15 || End <= 15
                    if ini == 0
                        xticks(ini:1:End+1);
                        xlim([ini,End+1]);
                    elseif End == 0
                        xticks(ini-1:1:End);
                        xlim([ini-1,End]);
                    end
                else
                    h.XAxis.MinorTick = 'on';
                end
                grid off
                axes('pos',[0.1 0.8 .3 .1])
                imshow('direzionescavoENG.png')
                hold off
                TempName = char(strcat('Report',siteID,'-', char(toolrif),'SezPCL_ENG.png'));
                saveas(ancora,TempName);
                ChartSEZPC1_ENG = Image(TempName);
                ChartSEZPC1_ENG.Style = {Height('6.6cm')};
                close(figure(3))
            end
            
            % ---Grafico Sezione Ultimo Mese---
            figure(4)
            clear nodisuZ
            clear nodisuY
            clear nodigiuZ
            clear nodigiuY
            clear nodistabZ
            clear nodistabY
            contaS_2 = 1;
            contaG_2 = 1;
            contaF_2 = 1;
            for disp = 1:rPCL(m,1)
                if diffZ(end,disp) > 0.5 % mm
                    nodisuZ(contaS_2,1) = CoordZ(disp,1); % il +1 salta l'ancora introdotta dopo l'esecuzione della funzione gemella
                    nodisuY(contaS_2,1) = CoordY(disp,1);
                    contaS_2 = contaS_2 + 1;
                elseif diffZ(end,disp) < -0.5
                    nodigiuZ(contaG_2,1) = CoordZ(disp,1);
                    nodigiuY(contaG_2,1) = CoordY(disp,1);
                    contaG_2 = contaG_2 + 1;
                else
                    nodistabZ(contaF_2,1) = CoordZ(disp,1);
                    nodistabY(contaF_2,1) = CoordY(disp,1);
                    contaF_2 = contaF_2 + 1;
                end
            end
            if cell2mat(CoordY(1,1)) > cell2mat(CoordY(end,1))
                plot([cell2mat(CoordY(:,1)); 0],[cell2mat(CoordZ(:,1)); 0],'-','Color','black','MarkerSize' ,7);
            else
                plot([0; cell2mat(CoordY(:,1))],[0; cell2mat(CoordZ(:,1))],'-','Color','black','MarkerSize' ,7)
            end
            hold on
            if contaS_2 > 1
                sezS = plot(cell2mat(nodisuY(:,1)),cell2mat(nodisuZ(:,1)),'^','Color','red','MarkerSize' ,7);
                set(sezS, 'markerfacecolor', get(sezS, 'color'));
            end
            if contaG_2 > 1
                sezG = plot(cell2mat(nodigiuY(:,1)),cell2mat(nodigiuZ(:,1)),'v','Color',[0.4660 0.6740 0.1880],'MarkerSize' ,7);
                set(sezG, 'markerfacecolor', get(sezG, 'color'));
            end
            if contaF_2 > 1
                sezN = plot(cell2mat(nodistabY(:,1)),cell2mat(nodistabZ(:,1)),'o','Color','black');
                set(sezN, 'markerfacecolor', get(sezN, 'color'));
            end
            ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
            set(ancora, 'markerfacecolor', get(ancora, 'color'));
            if contaS_2 > 1 && contaG_2 > 1 && contaF_2 > 1
                legend('Geometria dello strumento','Innalzamento','Abbassamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS_2 > 1 && contaG_2 > 1 && contaF_2 == 1
                legend('Geometria dello strumento','Innalzamento','Abbassamento','Ancora','Location','southoutside');
            elseif contaS_2 > 1 && contaG_2 == 1 && contaF_2 > 1
                legend('Geometria dello strumento','Innalzamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS_2 == 1 && contaG_2 > 1 && contaF_2 > 1
                legend('Geometria dello strumento','Abbassamento','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS_2 > 1 && contaG_2 == 1 && contaF_2 == 1
                legend('Geometria dello strumento','Innalzamento','Ancora','Location','southoutside');
            elseif contaS_2 == 1 && contaG_2 > 1 && contaF_2 == 1
                legend('Geometria dello strumento','Abbassamentoi','Nodi stabili','Ancora','Location','southoutside');
            elseif contaS_2 == 1 && contaG_2 == 1 && contaF_2 > 1
                legend('Geometria dello strumento','Nodi stabili','Ancora','Location','southoutside');
            end
            title('Direzione degli spostamenti - periodo di riferimento');
            xlabel('Progressiva [m]');
            ylabel('Direzione verticale');
            set(gca,'YTickLabel',[]);
            set(gca,'YTick',[]);
            ini = min([0; cell2mat(CoordY)]);
            End = max([0; cell2mat(CoordY)]);
            h = gca;
            if ini >= -15 || End <= 15
                if ini == 0
                    xticks(ini:1:End+1);
                    xlim([ini,End+1]);
                elseif End == 0
                    xticks(ini-1:1:End);
                    xlim([ini-1,End]);
                end
            else
                h.XAxis.MinorTick = 'on';
            end
            grid off
            axes('pos',[0.1 0.8 .3 .1])
            imshow('direzionescavo.png')
            hold off
            TempName = char(strcat('Report',siteID,'-', char(toolrif),'SezPCL2.png'));
            saveas(ancora,TempName);
            
            ChartSEZPC2 = Image(TempName);
            ChartSEZPC2.Style = {Height('6.6cm')};
            ChartSEZPC2_cap = Paragraph(['Fig. ' num2str(FIG) ' - Direzione degli spostamenti verticali '...
                'registrati durante il periodo di pre-convergenza e l''intervallo temporale di riferimento']);
            FIG = FIG+1;
            ChartSEZPC2_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
            lot2 = Table({ChartSEZPC1, ChartSEZPC2});
            lot2.Style = {ResizeToFitContents(false), Width('100%')};
            close(figure(4))
            
            if activeEN == 1
                % ---Grafico Sezione Ultimo Mese---
                figure(4)
                if cell2mat(CoordY(1,1)) > cell2mat(CoordY(end,1))
                    plot([cell2mat(CoordY(:,1)); 0],[cell2mat(CoordZ(:,1)); 0],'-','Color','black','MarkerSize' ,7);
                else
                    plot([0; cell2mat(CoordY(:,1))],[0; cell2mat(CoordZ(:,1))],'-','Color','black','MarkerSize' ,7)
                end
                hold on
                if contaS_2 > 1
                    sezS = plot(cell2mat(nodisuY(:,1)),cell2mat(nodisuZ(:,1)),'^','Color','red','MarkerSize' ,7);
                    set(sezS, 'markerfacecolor', get(sezS, 'color'));
                end
                if contaG_2 > 1
                    sezG = plot(cell2mat(nodigiuY(:,1)),cell2mat(nodigiuZ(:,1)),'v','Color',[0.4660 0.6740 0.1880],'MarkerSize' ,7);
                    set(sezG, 'markerfacecolor', get(sezG, 'color'));
                end
                if contaF_2 > 1
                    sezN = plot(cell2mat(nodistabY(:,1)),cell2mat(nodistabZ(:,1)),'o','Color','black');
                    set(sezN, 'markerfacecolor', get(sezN, 'color'));
                end
                ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
                set(ancora, 'markerfacecolor', get(ancora, 'color'));
                if contaS_2 > 1 && contaG_2 > 1 && contaF_2 > 1
                    legend('Array geometry','Upwards','Downwards','Stable node','Anchor','Location','southoutside');
                elseif contaS_2 > 1 && contaG_2 > 1 && contaF_2 == 1
                    legend('Array geometry','Upwards','Downwards','Anchor','Location','southoutside');
                elseif contaS_2 > 1 && contaG_2 == 1 && contaF_2 > 1
                    legend('Array geometry','Upwards','Stable node','Anchor','Location','southoutside');
                elseif contaS_2 == 1 && contaG_2 > 1 && contaF_2 > 1
                    legend('Array geometry','Downwards','Stable node','Anchor','Location','southoutside');
                elseif contaS_2 > 1 && contaG_2 == 1 && contaF_2 == 1
                    legend('Array geometry','Upwards','Anchor','Location','southoutside');
                elseif contaS_2 == 1 && contaG_2 > 1 && contaF_2 == 1
                    legend('Array geometry','Downwards','Stable node','Anchor','Location','southoutside');
                elseif contaS_2 == 1 && contaG_2 == 1 && contaF_2 > 1
                    legend('Array geometry','Stable node','Anchor','Location','southoutside');
                end
                title('Displacement direction - Reference time period');
                xlabel('Position [m]');
                ylabel('Vertical direction');
                set(gca,'YTickLabel',[]);
                set(gca,'YTick',[]);
                h = gca;
                if ini >= -15 || End <= 15
                    if ini == 0
                        xticks(ini:1:End+1);
                        xlim([ini,End+1]);
                    elseif End == 0
                        xticks(ini-1:1:End);
                        xlim([ini-1,End]);
                    end
                else
                    h.XAxis.MinorTick = 'on';
                end
                grid off
                axes('pos',[0.1 0.8 .3 .1])
                imshow('direzionescavoENG.png')
                hold off
                TempName = char(strcat('Report',siteID,'-', char(toolrif),'SezPCL2_ENG.png'));
                saveas(ancora,TempName);
                
                ChartSEZPC2_ENG = Image(TempName);
                ChartSEZPC2_ENG.Style = {Height('6.6cm')};
                ChartSEZPC2_ENG_cap = Paragraph(['Fig. ' num2str(FIG_ENG) ' - Vertical displacements direction '...
                    'observed during the pre-convergence phase and the reference time period']);
                FIG_ENG = FIG_ENG+1;
                ChartSEZPC2_ENG_cap.Style = {HAlign('center'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
                lot2_ENG = Table({ChartSEZPC1_ENG, ChartSEZPC2_ENG});
                lot2_ENG.Style = {ResizeToFitContents(false), Width('100%')};
                close(figure(4))
            end
            
            % --- grafico spostamenti cumulati - periodo di preconvergenza ---
            figure(5)
            if maxY < 0
                plot([0 Y],[0 Z_Cum_PC],'-','LineWidth',1,'Marker','.',...
                    'MarkerSize',15,'MarkerEdgeColor',[0.6350 0.0780 0.1840],'MarkerFaceColor',[0.6350 0.0780 0.1840]);
            else
                plot([Y 0],[Z_Cum_PC 0],'-','LineWidth',1,'Marker','.',...
                    'MarkerSize',15,'MarkerEdgeColor',[0.6350 0.0780 0.1840],'MarkerFaceColor',[0.6350 0.0780 0.1840]);
            end
            hold on
            ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
            set(ancora, 'markerfacecolor', get(ancora, 'color'));
            title('Spostamenti cumulati - Periodo di pre-convergenza');
            ylabel('Spostamento [mm]');
            xlabel('Progressiva [m]');
            h = gca;
            h.YAxis.MinorTick = 'on';
            grid on
            
            if maxY > 0 % mm
                xlim([0 maxY+1]);
            else
                xlim([Y(end)-1 0])
            end
            grid on
            
            iniY = min(min(Z_Cum_PC',Z_Cum'));
            endY = max(max(Z_Cum_PC',Z_Cum'));
            MAX = max(abs(iniY),endY);
            if MAX > 2.5 % Limiti Asse Y
                ylim([-MAX-1 MAX+1])
            else
                ylim([-2.5 +2.5]);
            end
            
            if ini >= -15 || End <= 15
                xticks(ini:1:End);
            else
                h.XAxis.MinorTick = 'on';
            end
            TempName3 = char(strcat('Report',siteID,'-', char(toolrif),'Z_Cum_PC.png'));
            saveas(ancora(1),TempName3);
            close(figure(5))
            
            % ENG
            % --- grafico spostamenti cumulati - periodo di preconvergenza ---
            if activeEN == 1
                figure(5)
                if maxY < 0
                    plot([0 Y],[0 Z_Cum_PC],'-','LineWidth',1,'Marker','.',...
                        'MarkerSize',15,'MarkerEdgeColor',[0.6350 0.0780 0.1840],'MarkerFaceColor',[0.6350 0.0780 0.1840]);
                else
                    plot([Y 0],[Z_Cum_PC 0],'-','LineWidth',1,'Marker','.',...
                        'MarkerSize',15,'MarkerEdgeColor',[0.6350 0.0780 0.1840],'MarkerFaceColor',[0.6350 0.0780 0.1840]);
                end
                hold on
                ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
                set(ancora, 'markerfacecolor', get(ancora, 'color'));
                title('Cumulative displacements - Pre-convergence phase');
                ylabel('Displacement [mm]');
                xlabel('Position [m]');
                h = gca;
                h.YAxis.MinorTick = 'on';
                grid on
                
                if MAX > 2.5 % Limiti Asse Y
                    ylim([-MAX-1 MAX+1])
                else
                    ylim([-2.5 +2.5]);
                end
                
                if ini >= -15 || End <= 15
                    xticks(ini:1:End);
                else
                    h.XAxis.MinorTick = 'on';
                end
                TempName3_ENG = char(strcat('Report',siteID,'-', char(toolrif),'Z_Cum_PC_ENG.png'));
                saveas(ancora(1),TempName3_ENG);
                close(figure(5))
            end
            
            % --- grafico spostamenti cumulati - Periodo di riferimento ---
            figure(6)
            if maxY < 0
                plot([0 Y],[0 Z_Cum],'-','LineWidth',1,'Marker','.',...
                    'MarkerSize',15,'MarkerEdgeColor','b','MarkerFaceColor',[0.5,0.5,0.5]);
            else
                plot([Y 0],[Z_Cum 0],'-','LineWidth',1,'Marker','.',...
                    'MarkerSize',15,'MarkerEdgeColor','b','MarkerFaceColor',[0.5,0.5,0.5]);
            end
            hold on
            ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
            set(ancora, 'markerfacecolor', get(ancora, 'color'));
            title('Spostamenti cumulati - periodo di riferimento');
            ylabel('Spostamento [mm]');
            xlabel('Progressiva [m]');
            h = gca;
            h.YAxis.MinorTick = 'on';
            grid on
            
            if MAX > 2.5 % Limiti Asse Y
                ylim([-MAX-1 MAX+1])
            else
                ylim([-2.5 +2.5]);
            end
            if ini >= -15 || End <= 15
                xticks(ini:1:End);
            else
                h.XAxis.MinorTick = 'on';
            end
            TempName4 = char(strcat('Report',siteID,'-', char(toolrif),'Z_Cum_PCL.png'));
            saveas(ancora(1),TempName4);
            close(figure(6))
            
            ChartPCL3 = Image(TempName3);
            ChartPCL4 = Image(TempName4);
            lot3 = Table({ChartPCL3, ChartPCL4});
            lot3.Style = {ResizeToFitContents(false), Width('100%')};
            ChartPCL3.Style = {Height('6.6cm'),HAlign('center')};
            ChartPCL4.Style = {Height('6.6cm'),HAlign('center')};
            ChartPCL3_cap = Paragraph(['Fig. ' num2str(FIG) ' - Spostamenti '...
                'differenziali cumulati in direzione verticale registrati durante '...
                'il periodo di pre-convergenza e il periodo di riferimento']);
            ChartPCL3_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
            FIG = FIG+1;
            
            % ENG
            if activeEN == 1
                % --- grafico spostamenti cumulati - Periodo di riferimento ---
                figure(6)
                if maxY < 0
                    plot([0 Y],[0 Z_Cum],'-','LineWidth',1,'Marker','.',...
                        'MarkerSize',15,'MarkerEdgeColor','b','MarkerFaceColor',[0.5,0.5,0.5]);
                else
                    plot([Y 0],[Z_Cum 0],'-','LineWidth',1,'Marker','.',...
                        'MarkerSize',15,'MarkerEdgeColor','b','MarkerFaceColor',[0.5,0.5,0.5]);
                end
                hold on
                ancora = plot(0,0,'s','Color','black','MarkerSize' ,13);
                set(ancora, 'markerfacecolor', get(ancora, 'color'));
                title('Cumulative displacements - Reference time period');
                ylabel('Displacement [mm]');
                xlabel('Position [m]');
                h = gca;
                h.YAxis.MinorTick = 'on';
                grid on
                
                if MAX > 2.5 % Limiti Asse Y
                    ylim([-MAX-1 MAX+1])
                else
                    ylim([-2.5 +2.5]);
                end
                if ini >= -15 || End <= 15
                    xticks(ini:1:End);
                else
                    h.XAxis.MinorTick = 'on';
                end
                TempName4_ENG = char(strcat('Report',siteID,'-', char(toolrif),'Z_Cum_PCL_ENG.png'));
                saveas(ancora(1),TempName4_ENG);
                close(figure(6))
                
                ChartPCL3_ENG = Image(TempName3_ENG);
                ChartPCL4_ENG = Image(TempName4_ENG);
                lot3_ENG = Table({ChartPCL3_ENG, ChartPCL4_ENG});
                lot3_ENG.Style = {ResizeToFitContents(false), Width('100%')};
                ChartPCL3_ENG.Style = {Height('6.6cm'),HAlign('center')};
                ChartPCL4_ENG.Style = {Height('6.6cm'),HAlign('center')};
                ChartPCL3_ENG_cap = Paragraph(['Fig. ' num2str(FIG_ENG) ' - Cumulative differential displacements '...
                    'recorded in vertical direction during the pre-convergence phase and '...
                    'the reference time period']);
                ChartPCL3_ENG_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
                FIG_ENG = FIG_ENG+1;
            end
            
            % testi
            sezPCL = Heading3('PreConv Link');
            sezPCL.Style = {FontSize(Font_tools),Bold(1),Italic(1)};
            PCL = Section();
            PCL.Title = sezPCL;
            testo1 = Paragraph(['I grafici riportati di seguito mostrano l''entit' char(224) ' e il verso degli spostamenti '...
                'locali e cumulati in direzione verticale, misurati dal sensore MEMS 3D nel periodo di pre-convergenza '...
                '(ovvero considerando l''intervallo temporale fra la data di riferimento indicata in Tab. 1 '...
                'e il ' datestr(datenum(data_PC),'dd-mm-yyyy') ') e di riferimento. In particolare, valori negativi '...
                'indicano un abbassamento del nodo, mentre spostamenti '...
                'positivi caratterizzano un movimento verso l''alto del sensore in oggetto durante '...
                'l''intervallo di tempo considerato.']);
            % Locale ultimo mese
            progZ = max(abs(Z_Loc));
            prY = find(abs(Z_Loc)==progZ);
            progY = Y(prY(1));
            % Locale da sempre
            progZ_Tot = max(abs(Z_Loc_Tot));
            prY_Tot = find(abs(Z_Loc_Tot)==progZ_Tot);
            progY_Tot = Y(prY_Tot(1));
            % locale periodo pre-convergenza
            progZ_PC = max(abs(Z_Loc_PC));
            prY_PC = find(abs(Z_Loc_PC)==progZ_PC);
            progY_PC = Y(prY_PC(1));
            testo2 = Paragraph(['Il massimo valore di spostamento differenziale locale registrato durante il periodo temporale '...
                'di riferimento '  char(232) ' pari a ' num2str(max(abs(round(Z_Loc,1)))) ' mm, rilevato dal nodo '...
                'alla progressiva di ' num2str(progY) ' m. La stessa analisi, condotta relativamente al periodo di pre-convergenza '...
                'e all''intera durata del monitoraggio, evidenzia movimenti differenziali locali pari a ' num2str(max(abs(round(Z_Loc_PC,1)))) ...
                ' e ' num2str(max(abs(round(Z_Loc_Tot,1)))) ' mm, registrati rispettivamente dai nodi alla progressiva di ' num2str(progY_PC) ...
                ' e ' num2str(progY_Tot) ' m.']);
            parte1 = ['La figura associata riassume la direzione di movimento verticale dei singoli nodi '...
                'posizionati secondo la geometria dello strumento. Si noti che, in questa rappresentazione '...
                'vengono considerati stabili i sensori caratterizzati da uno spostamento '...
                'inferiore a 0.5 mm. Nel periodo di pre-convergenza, '...
                'l''analisi ha evidenziato ' num2str(contaS-1) ' '];
            if contaS-1 == 1
                parte2 = 'nodo caratterizzato ';
            else
                parte2 = 'nodi caratterizzati ';
            end
            parte3 = ['da movimenti verso l''alto, ' num2str(contaG-1) ' verso il basso e ' num2str(contaF-1) ];
            if contaF-1 == 1
                parte4 = ' nodo stabile.  ';
            else
                parte4 = ' nodi stabili. ';
            end
            parte5 = ['Le corrispondenti informazioni relative all''intervallo di '...
                'riferimento forniscono ' num2str(contaS_2-1) ];
            if contaS_2-1 == 1
                parte6 = ' sensore che si sposta ';
            else
                parte6 = ' sensori che si spostano ';
            end
            parte7 = ['verso l''alto, ' num2str(contaG_2-1) ' verso il basso e ' num2str(contaF_2-1) ];
            if contaF_2-1 == 1
                parte8 = ' stabile.';
            else
                parte8 = ' stabili.';
            end
            testo3 = Paragraph([parte1 parte2 parte3 parte4 parte5 parte6 parte7 parte8]);
            spazio = Paragraph('');
            testo1.HAlign = 'justify';
            testo2.HAlign = 'justify';
            testo3.HAlign = 'justify';
            add(DT,sezPCL);
            add(DT,testo1);
            add(DT,spazio);
            add(DT,testo2);
            add(DT,lot1);
            add(DT,ChartPCL_cap);
            add(DT,testo3);
            add(DT,lot2);
            add(DT,ChartSEZPC2_cap);
            add(DT,lot3);
            add(DT,ChartPCL3_cap);
            add(DT,br);
            
            % ENG
            if activeEN == 1
                % testi
                sezPCL_ENG = Heading3('PreConv Link');
                sezPCL_ENG.Style = {FontSize(Font_tools),Bold(1),Italic(1)};
                PCL_ENG = Section();
                PCL_ENG.Title = sezPCL_ENG;
                testo1 = Paragraph(['Following graphs display the magnitude and direction '...
                    'of local and cumulative displacements '...
                    'measured along the vertical direction by the 3D MEMS sensor during the pre-convergence phase '...
                    '(referring to the time interval between the reference date reported in Tab. 1 '...
                    'and the following date: ' datestr(datenum(data_PC),'dd-mm-yyyy') ') and the reference time period. '...
                    'In particular, negative values correspond to downwards movements of the node, '...
                    'while positive ones represent an upward displacement.']);
                testo2 = Paragraph(['The maximum local differential displacement value recorded during the reference time period is '...
                    num2str(max(abs(round(Z_Loc,1)))) ' mm, measured by the node '...
                    'located at ' num2str(progY) ' m from the fixed point. The same analysis performed on the pre-convergence phase '...
                    'and on the complete monitoring period highlights local differential displacement values of ' num2str(max(abs(round(Z_Loc_PC,1)))) ...
                    ' and ' num2str(max(abs(round(Z_Loc_Tot,1)))) ' mm, recorded by nodes located respectively at ' num2str(progY_PC) ...
                    ' m and ' num2str(progY_Tot) ' m from the fixed point.']);
                parte1 = ['The corresponding figure summarizes the vertical displacement direction of each single node, according '...
                    'to their position along the Array. In this specific representation, sensors featuring displacement values less than 0.5 mm '...
                    'are assumed as stable. During the pre-convergence phase, the analysis evidenced ' num2str(contaS-1) ' '];
                if contaS-1 == 1
                    parte2 = 'node featuring ';
                else
                    parte2 = 'nodes featuring ';
                end
                parte3 = ['upward movement, ' num2str(contaG-1) ' in downward direction, and ' num2str(contaF-1) ];
                if contaF-1 == 1
                    parte4 = ' stable sensor.  ';
                else
                    parte4 = ' stable sensors. ';
                end
                parte5 = ['By taking into account the reference time period, the '...
                    'results show ' num2str(contaS_2-1) ];
                if contaS_2-1 == 1
                    parte6 = ' sensor moving ';
                else
                    parte6 = ' sensors moving ';
                end
                parte7 = ['upwards, ' num2str(contaG_2-1) ' in downward direction, and ' num2str(contaF_2-1) ];
                if contaF_2-1 == 1
                    parte8 = ' stable node.';
                else
                    parte8 = ' stable nodes.';
                end
                testo3 = Paragraph([parte1 parte2 parte3 parte4 parte5 parte6 parte7 parte8]);
                spazio = Paragraph('');
                testo1.HAlign = 'justify';
                testo2.HAlign = 'justify';
                testo3.HAlign = 'justify';
                add(DT_ENG,sezPCL_ENG);
                add(DT_ENG,testo1);
                add(DT_ENG,spazio);
                add(DT_ENG,testo2);
                add(DT_ENG,lot1_ENG);
                add(DT_ENG,ChartPCL_ENG_cap);
                add(DT_ENG,testo3);
                add(DT_ENG,lot2_ENG);
                add(DT_ENG,ChartSEZPC2_ENG_cap);
                add(DT_ENG,lot3_ENG);
                add(DT_ENG,ChartPCL3_ENG_cap);
                add(DT_ENG,br);
            end
        else
            if ATTIVA == 1
                testo = Paragraph('Non sono presenti dati relativi al periodo temporale considerato.');
                ATTIVA = 0;
                check = 1;
                add(DT,testo);
                if activeEN == 1
                    testo = Paragraph('No data available for the reference time period.');
                    add(DT_ENG,testo);
                end
            end
        end
    end
end

text = 'report_PCL function executed correctly';
fprintf(fileID,fmt,text);
fclose(fileID);

end