% Funzione che legge i valori di calibrazione per i vari sensori

function [DCalTuLTot,DCalRLTot,DCalTLHTot,DCalTLHRHTot,DCalPCLTot,DCalPCLHRTot,...
    DCalPLTot,DCalLLTot,DCalELTot,DCalEL3DTot,DCalWELTot,DCalMPBELTot,...
    DCalALTot,DCalCrLTot,DCal3DCrLTot,DCal2DCrLTot,DCalSMTot,yesTuL,yesRL,yesTLH,yesTLHRH,...
    yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesCrL,yes3DCrL,yes2DCrL,...
    yesAL,yesSM] = letturaCal(IDcentralina,DTcatena,catena,rTuL,rRL,rTLH,rTLHRH,...
    rPCL,rPCLHR,rPrL,rLL,rEL,r3DEL,rWEL,rMPBEL,NumBasi,rAL,rCrL,r3DCrL,r2DCrL,rSM,...
    NodoTunnelLink,NodoRadialLink,NodoTiltLinkH,NodoPreConvLink,NodoPressureLink,...
    NodoLoadLink,NodoExtensometerLink,Nodo3DExtensometerLink,...
    NodoWireExtensometerLink,NodoMultiPointRodExtensometer,NodoAnalogLink,...
    NodoCrackLink,Nodo3DCrackLink,Nodo2DCrackLink,NodoStressMeter,yesTuL,yesRL,yesTLH,yesTLHRH,...
    yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesAL,yesCrL,yes3DCrL,yes2DCrL,...
    yesSM,conn,FileName)

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

% Inizializzo le matrici
[rC,~] = size(catena);
idNodo = cell2mat(catena(:,1));

% Tunnel Link
if isempty(NodoTunnelLink) == 1
    DCalTuLTot = [];
else
    if strcmp(NodoTunnelLink(1,4),'ADC') || strcmp(NodoTunnelLink{1,4},'null') || isempty(NodoTunnelLink{1,4}) == 1
        DCalTuLTot = cell(rTuL,11);
    elseif strcmp(NodoTunnelLink(1,4),'g')
        DCalTuLTot = [];
    end
end

% Radial Link
if isempty(NodoRadialLink) == 1
    DCalRLTot = [];
else
    if strcmp(NodoRadialLink(1,4),'ADC') || strcmp(NodoRadialLink{1,4},'null') || isempty(NodoRadialLink{1,4}) == 1
        DCalRLTot = cell(rRL,11);
    elseif strcmp(NodoRadialLink(1,4),'g')
        DCalRLTot = [];
    end
end

% Tilt Link H
if isempty(NodoTiltLinkH) == 1
    DCalTLHTot = [];
else
    if strcmp(NodoTiltLinkH(1,4),'ADC') || strcmp(NodoTiltLinkH{1,4},'null') || isempty(NodoTiltLinkH{1,4}) == 1
        DCalTLHTot = cell(rTLH,11);
    elseif strcmp(NodoTiltLinkH(1,4),'Gradi') || strcmp(NodoTiltLinkH(1,4),'g')
        DCalTLHTot = [];
    end
end

% Tilt Link HR H
DCalTLHRHTot = cell(rTLHRH,4); % matrice che concatena i dati di calibrazione relativi a ciascun nodo Tilt Link HR H

% PreConv Link
if isempty(NodoPreConvLink) == 1
    DCalPCLTot = [];
else
    if strcmp(NodoPreConvLink(1,4),'ADC') || strcmp(NodoPreConvLink{1,4},'null') || isempty(NodoPreConvLink{1,4}) == 1
        DCalPCLTot = cell(rPCL,11);
    elseif strcmp(NodoPreConvLink(1,4),'g')
        DCalPCLTot = [];
    end
end

% PreConv Link HR
DCalPCLHRTot = cell(rPCLHR,4); % matrice che concatena i dati di calibrazione relativi a ciascun nodo PreConv Link HR

% Pressure Link
x = isempty(NodoPressureLink);
if x == 0
    for P = 1:rPrL
        if strcmp(NodoPressureLink(P,3),'4-20 mA')
            DCalPLTot = cell(rPrL,3); % matrice che concatena i dati relativi a ciascun nodo Pressure Link
            break
        elseif strcmp(NodoPressureLink(P,3),'mV/V')
            DCalPLTot = cell(rPrL,2); % matrice che concatena i dati relativi a ciascun nodo Pressure Link
        end
    end
else
    DCalPLTot = [];
end

% Load Link
x = isempty(NodoLoadLink);
if x == 0
    for P = 1:rLL
        if strcmp(NodoLoadLink(P,3),'4-20 mA')
            DCalLLTot = cell(rLL,3); % matrice che concatena i dati relativi a ciascun nodo Load Link
            break
        elseif strcmp(NodoLoadLink(P,3),'mV/V') || strcmp(NodoLoadLink(P,3),'ADC')
            DCalLLTot = cell(rLL,2); % matrice che concatena i dati relativi a ciascun nodo Load Link
        end
    end
else
    DCalLLTot = [];
end

% Extensometer Link
x = isempty(NodoExtensometerLink);
if x == 0
    for P = 1:rEL
        if strcmp(NodoExtensometerLink(P,3),'4-20 mA')
            DCalELTot = cell(rEL,4);
            break
        elseif strcmp(NodoExtensometerLink(P,3),'mV/V') || strcmp(NodoExtensometerLink(P,3),'Hz')
            DCalELTot = cell(rEL,3);
        end
    end
else
    DCalELTot = [];
end

% 3D Extensometer Link
x = isempty(Nodo3DExtensometerLink);
if x == 0
    for P = 1:r3DEL
        if strcmp(Nodo3DExtensometerLink(P,3),'4-20 mA')
            DCalEL3DTot = cell(rEL,9);
            break
        elseif strcmp(Nodo3DExtensometerLink(P,3),'mV/V') || strcmp(Nodo3DExtensometerLink(P,3),'Hz')
            DCalEL3DTot = cell(r3DEL,6);
        end
    end
else
    DCalEL3DTot = [];
end

% Wire Extensometer Link
x = isempty(NodoWireExtensometerLink);
if x == 0
    for P = 1:rWEL
        if strcmp(NodoWireExtensometerLink(P,3),'4-20 mA')
            DCalWELTot = cell(rWEL,3);
            break
        elseif strcmp(NodoWireExtensometerLink(P,3),'mV/V') || strcmp(NodoWireExtensometerLink(P,3),'Hz')
            DCalWELTot = cell(rWEL,2);
        end
    end
else
    DCalWELTot = [];
end

% Estensimetri multibase
x = isempty(NodoMultiPointRodExtensometer);
if x == 0
    for P = 1:rMPBEL
        if strcmp(NodoMultiPointRodExtensometer(P,3),'mV/V')
            DCalMPBELTot = cell(rMPBEL,NumBasi); % matrice che concatena i dati relativi a ciascun nodo Multi Point Borehole Extensometer Link
            break
        end
    end
else
    DCalMPBELTot = [];
end

% Analog Link
DCalALTot = cell(rAL,3); % matrice che concatena i dati relativi a ciascun nodo Analog Link

% Crack Link
x = isempty(NodoCrackLink);
if x == 0
    for P = 1:rCrL  
        if strcmp(NodoCrackLink(P,3),'Hz') || strcmp(NodoCrackLink(P,3),'4-20 mA')||strcmp(NodoCrackLink(P,3),'Hz mm')
            DCalCrLTot = cell(rCrL,3); % matrice che concatena i dati relativi a ciascun nodo Crack Link
        elseif strcmp(NodoCrackLink(P,3),'mV/V')
            DCalCrLTot = cell(rCrL,2); % matrice che concatena i dati relativi a ciascun nodo Crack Link
        end
    end
else
    DCalCrLTot = [];
end

% 3D Crack Link
x = isempty(Nodo3DCrackLink);
if x == 0
    for P = 1:r3DCrL  
        if strcmp(Nodo3DCrackLink(P,3),'Hz') || strcmp(Nodo3DCrackLink(P,3),'4-20 mA')
            DCal3DCrLTot = cell(r3DCrL,9); % matrice che concatena i dati relativi a ciascun nodo 3D Crack Link
        elseif strcmp(Nodo3DCrackLink(P,3),'mV/V')
            DCal3DCrLTot = cell(r3DCrL,6); % matrice che concatena i dati relativi a ciascun nodo 3D Crack Link
        end
    end
else
    DCal3DCrLTot = [];
end

% 2D Crack Link
x = isempty(Nodo2DCrackLink);
if x == 0
    for P = 1:r2DCrL  
        if strcmp(Nodo2DCrackLink(P,3),'Hz') || strcmp(Nodo2DCrackLink(P,3),'4-20 mA')
            DCal2DCrLTot = cell(r2DCrL,6); % matrice che concatena i dati relativi a ciascun nodo 2D Crack Link
        elseif strcmp(Nodo2DCrackLink(P,3),'mV/V')
            DCal2DCrLTot = cell(r2DCrL,4); % matrice che concatena i dati relativi a ciascun nodo 2D Crack Link
        end
    end
else
    DCal2DCrLTot = [];
end

% Stress Meter
x = isempty(NodoStressMeter);
if x == 0
    for P = 1:rSM 
        if strcmp(NodoStressMeter(P,3),'Hz')
            DCalSMTot = cell(rSM,1); % matrice che concatena i dati relativi a ciascun nodo Stress Meter
        end
    end
else
    DCalSMTot = [];
end

%% Contatori
% Matrice con testi di conferma lettura o warning di lettura per le varie tipologie di nodi
TuL = 1; % Tunnel Link
RL = 1; % Radial Link
TLH = 1; % Tilt Link H
TLHRH = 1; % Tilt Link HR H
PCL = 1; % PreConv Link
PCLHR = 1; % PreConv Link HR
PL = 1; % Pressure Link
LL = 1; % Load Link
EL = 1; % Extensometer Link
EL3D = 1; % 3D Extensometer Link
WEL = 1; % Wire Extensometer Link
MPBEL = 1; % Multi Point Borehole Extensometer Link
AL = 1; % Analog Link
CrL = 1; % Crack Link
CrL3D = 1; % 3D Crack Link
CrL2D = 1; % 2D Crack Link
SM = 1; % Stress Meter

for a = 1:rC
    nNodo = mat2str(cell2mat(catena(a,3)));

    if idNodo(a,1) == 17 % Tunnel Link
        if strcmp(NodoTunnelLink(1,4),'ADC') || strcmp(NodoTunnelLink{1,4},'null') || isempty(NodoTunnelLink{1,4}) == 1
            %% Tunnel link
            comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5, Cal6, Cal7, Cal8, CalA, CalB from CalibrationView where UnitName = ''' ...
                IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DCalTuL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Tunnel Link
            [rr,cc] = size(DCalTuL);
            if rr==1 && cc==1
                text = ['Warning: there was an error reading node Tunnel Link number ' nNodo ''];
                fprintf(fileID,fmt,text);
                yesTuL = 0;
            else
                DCalTuLTot(TuL,:) = DCalTuL(:,:);
                TuL = TuL+1;
                text = ['Calibration of node Tunnel Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
            end
        else
            ok = ['Calibration parameters of node Tunnel Link number ' nNodo ' unnecessary'];
            fprintf(fileID,fmt,ok);
        end

    elseif idNodo(a,1) == 18 % Radial Link
        if strcmp(NodoRadialLink(1,4),'ADC') || strcmp(NodoRadialLink{1,4},'null') || isempty(NodoRadialLink{1,4}) == 1
            %% Radial Link
            comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5, Cal6, Cal7, Cal8, CalA, CalB from CalibrationView where UnitName = ''' ...
                IDcentralina ''' and ToolNameID = ''' ...
                DTcatena ''' and NodeNum = ''' nNodo ''''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DCalRL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Radial Link
            [rr,cc] = size(DCalRL);
            if rr==1 && cc==1
                text = ['Warning: there was an error reading node Radial Link number ' nNodo ''];
                fprintf(fileID,fmt,text);
                yesRL = 0;
            else
                DCalRLTot(RL,:) = DCalRL(:,:);
                RL = RL+1;
                text = ['Calibration of node Radial Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
            end
        else
            ok = ['Calibration parameters of node Radial Link number ' nNodo ' unnecessary'];
            fprintf(fileID,fmt,ok);
        end

    elseif idNodo(a,1) == 11 % Tilt Link H
    %% Tilt Link H    
       if strcmp(NodoTiltLinkH(1,4),'ADC') || strcmp(NodoTiltLinkH{1,4},'null') || isempty(NodoTiltLinkH{1,4}) == 1
            %% Importazione dei dati di calibrazione per Tilt link H
            comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5, Cal6, Cal7, Cal8, CalA, CalB from CalibrationView where UnitName = '''...
                IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DCalTLH = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Tilt Link H
            [rr,cc] = size(DCalTLH);
            if rr==1 && cc==1
                w = ['Warning: there was an error reading calibration values of node Tilt Link H number ' nNodo ''];
                fprintf(fileID,fmt,w);
                yesTLH = 0;
            else
                DCalTLHTot(TLH,:) = DCalTLH;
                TLH = TLH+1;
                ok = ['Calibration parameters of node Tilt Link H number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,ok);
            end
        else
            ok = ['Calibration parameters of node Tilt Link H number ' nNodo ' unnecessary'];
            fprintf(fileID,fmt,ok);
        end

    elseif idNodo(a,1) == 12 % Tilt Link HR H
    %% Tilt Link HR H    
       comando = ['select Cal0, Cal1, Cal2, Cal3 from CalibrationView where UnitName = ''' ...
           IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''''];
       curs = exec(conn,comando);
       curs = fetch(curs);
       DCalTLHRH = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Tilt Link HR H
       [rr,cc] = size(DCalTLHRH);
       if rr==1 && cc==1
          text = ['Warning: there was an error reading node Tilt Link HR H number ' nNodo ''];
          fprintf(fileID,fmt,text);
          yesTLHRH = 0;
       else    
           DCalTLHRHTot(TLHRH,:) = DCalTLHRH(:,:);
           TLHRH = TLHRH+1;
           text = ['Calibration of node Tilt Link HR H number ' nNodo ' downloaded correctly'];
           fprintf(fileID,fmt,text);
       end

    elseif idNodo(a,1) == 23 % PreConv Link
        if strcmp(NodoPreConvLink(1,4),'ADC') || strcmp(NodoPreConvLink{1,4},'null') || isempty(NodoPreConvLink{1,4}) == 1
            %% PreConv Link
            comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5, Cal6, Cal7, Cal8, CalA, CalB from CalibrationView where UnitName = ''' ...
                IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''''];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DCalPCL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo PreConv Link
            [rr,cc] = size(DCalPCL);
            if rr==1 && cc==1
                text = ['Warning: there was an error reading node PreConv Link number ' nNodo ''];
                fprintf(fileID,fmt,text);
                yesPCL = 0;
            else
                DCalPCLTot(PCL,:) = DCalPCL(:,:);
                PCL = PCL+1;
                text = ['Calibration of node PreConv Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
            end
        else
            ok = ['Calibration parameters of node PreConv Link number ' nNodo ' unnecessary'];
            fprintf(fileID,fmt,ok);
        end

    elseif idNodo(a,1) == 24 % PreConv Link HR
    %% PreConv Link HR    
       comando = ['select Cal0, Cal1, CalA, CalB from CalibrationView where UnitName = ''' ...
           IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''''];
       curs = exec(conn,comando);
       curs = fetch(curs);
       DCalPCLHR = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo PreConv Link HR
       [rr,cc] = size(DCalPCLHR);
       if rr==1 && cc==1
          text = ['Warning: there was an error reading node PreConv Link HR number ' nNodo ''];
          fprintf(fileID,fmt,text);
          yesPCLHR = 0;
       else    
           DCalPCLHRTot(PCLHR,:) = DCalPCLHR(:,:);
           PCLHR = PCLHR+1;
           text = ['Calibration of node PreConv Link HR number ' nNodo ' downloaded correctly'];
           fprintf(fileID,fmt,text);
       end

    elseif idNodo(a,1) == 21 % Pressure Link
       %% Pressure Link 
           if strcmp(NodoPressureLink(PL,3),'4-20 mA')
              comando = ['select Cal0, Cal1, Cal2 from CalibrationView where UnitName = ''' ...
              IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(NodoPressureLink(PL,3),'mV/V')
               comando = ['select Cal0 from CalibrationView where UnitName = ''' ...
                IDcentralina ''' andToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalPL = curs.Data; % matrice che raccoglie i dati del nodo numero 'nNodo' di tipo Pressure Link
           [rr,cc] = size(DCalPL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Pressure Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesPL = 0;
           else
                if strcmp(NodoPressureLink(PL,3),'mV/V')
                    DCalPLTot(PL,1) = DCalPL(:,:);
                elseif strcmp(NodoPressureLink(PL,3),'4-20 mA')
                        DCalPLTot(PL,1:3) = DCalPL(:,:);
                end
                PL = PL+1;
                text = ['Calibration of node Pressure Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end

     elseif idNodo(a,1) == 15 % Load Link
         %% Importazione dei dati di calibrazione per Load Link 
            if strcmp(NodoLoadLink(LL,3),'mV/V') || strcmp(NodoLoadLink(a,3),'ADC')
              comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                  IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
            elseif strcmp(NodoLoadLink(LL,3),'4-20 mA')
                   comando = ['select Cal0, Cal1, Cal2 from CalibrationView where UnitName = ''' ...
                       IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
            end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalLL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Load Link
           [rr,cc] = size(DCalLL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Load Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesLL = 0;
           else
                if strcmp(NodoLoadLink(LL,3),'mV/V') || strcmp(NodoLoadLink(LL,3),'ADC')
                    DCalLLTot(LL,1:2) = DCalLL(:,:);
                elseif strcmp(NodoLoadLink(LL,3),'4-20 mA')
                       DCalLLTot(LL,1:3) = DCalLL(:,:);
                end
                LL = LL+1;
                text = ['Calibration of node Load Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end
    
    elseif idNodo(a,1) == 16 % Extensometer Link
           %% Importazione dei dati di calibrazione per Extensometer Link 
           if strcmp(NodoExtensometerLink(EL,3),'Hz') || strcmp(NodoExtensometerLink(EL,3),'mV/V')
               comando = ['select Cal0, Cal1, carico_iniziale from CalibrationView where UnitName = ''' ...
                        IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(NodoExtensometerLink(EL,3),'4-20 mA')
                  comando = ['select Cal0, Cal1, Cal2, carico_iniziale from CalibrationView where UnitName = ''' ...
                        IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalEL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Extensometer Link
           [rr,cc] = size(DCalEL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Extensometer Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesEL = 0;
           else
               if strcmp(NodoExtensometerLink(EL,3),'Hz') || strcmp(NodoExtensometerLink(EL,3),'mV/V')
                   DCalELTot(EL,1:3) = DCalEL(:,:);
               elseif strcmp(NodoExtensometerLink(EL,3),'4-20 mA')
                      DCalELTot(EL,1:4) = DCalEL(:,:);
               end
               EL = EL+1;
               text = ['Calibration of node Extensometer Link number ' nNodo ' downloaded correctly'];
               fprintf(fileID,fmt,text);
           end 
           
     elseif idNodo(a,1) == 19 % 3D Extensometer Link
           %% Importazione dei dati di calibrazione per 3D Extensometer Link 
           if strcmp(Nodo3DExtensometerLink(EL3D,3),'Hz') || strcmp(Nodo3DExtensometerLink(EL3D,3),'mV/V')
                  comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(Nodo3DExtensometerLink(EL3D,3),'4-20 mA')
                  comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5, Cal6, Cal7, Cal8 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalEL3D = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Extensometer Link
           [rr,cc] = size(DCalEL3D);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node 3D Extensometer Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yes3DEL = 0;
           else
               if strcmp(Nodo3DExtensometerLink(EL3D,3),'Hz') || strcmp(Nodo3DExtensometerLink(EL3D,3),'mV/V')
                   DCalEL3DTot(EL3D,1:6) = DCalEL3D(:,:);
               elseif strcmp(Nodo3DExtensometerLink(EL3D,3),'4-20 mA')
                   DCalEL3DTot(EL3D,1:9) = DCalEL3D(:,:);
               end
                EL3D = EL3D+1;
                text = ['Calibration of node 3D Extensometer Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end 
               
    elseif idNodo(a,1) == 22 % Wire Extensometer Link
           %% Importazione dei dati di calibrazione per Wire Extensometer Link 
           if strcmp(NodoWireExtensometerLink(WEL,3),'4-20 mA')
              comando = ['select Cal0, Cal1, Cal2 from CalibrationView where UnitName = ''' ...
                IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(NodoWireExtensometerLink(WEL,3),'mV/V')
                  comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalWEL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Wire Extensometer Link
           [rr,cc] = size(DCalWEL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Wire Extensometer Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesWEL = 0;
           else
               if strcmp(NodoWireExtensometerLink(WEL,3),'4-20 mA')
                   DCalWELTot(WEL,1:3) = DCalWEL(:,:);
               elseif strcmp(NodoWireExtensometerLink(WEL,3),'mV/V')
                   DCalWELTot(WEL,1:2) = DCalWEL(:,:);
               end
                WEL = WEL+1;
                text = ['Calibration of node Wire Extensometer Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end 
           
   elseif idNodo(a,1) == 25 % Multi Point Borehole Extensometer Link
           %% Importazione dei dati di calibrazione per Multi Point Borehole Extensometer Link 
           if strcmp(NodoMultiPointRodExtensometer(MPBEL,3),'mV/V')
               if NumBasi == 1
                   comando = ['select Cal0 from CalibrationView where UnitName = ''' ...
                       IDcentralina ''' and ToolNameID = ''' ...
                        DTcatena ''' and NodeNum = ''' nNodo ''' '];
               elseif NumBasi == 2
                      comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                          IDcentralina ''' and ToolNameID = ''' ...
                          DTcatena ''' and NodeNum = ''' nNodo ''' '];
               elseif NumBasi == 3
                      comando = ['select Cal0, Cal1, Cal2 from CalibrationView where UnitName = ''' ...
                          IDcentralina ''' and ToolNameID = ''' ...
                          DTcatena ''' and NodeNum = ''' nNodo ''' '];
               elseif NumBasi == 4
                      comando = ['select Cal0, Cal1, Cal2, Cal3 from CalibrationView where UnitName = ''' ...
                          IDcentralina ''' and ToolNameID = ''' ...
                          DTcatena ''' and NodeNum = ''' nNodo ''' '];
               elseif NumBasi == 5
                      comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4 from CalibrationView where UnitName = ''' ...
                          IDcentralina ''' and ToolNameID = ''' ...
                          DTcatena ''' and NodeNum = ''' nNodo ''' '];
               end
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalMPBEL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Multi Point Borehole Extensometer Link
           [rr,cc] = size(DCalMPBEL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node MultiPoint Borehole Extensometer Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesMPBEL = 0;
           else
               if strcmp(NodoMultiPointRodExtensometer(MPBEL,3),'mV/V')
                  if NumBasi == 1
                      DCalMPBELTot(MPBEL,1) = DCalMPBEL(:,:);
                  elseif NumBasi == 2
                      DCalMPBELTot(MPBEL,1:2) = DCalMPBEL(:,:);
                  elseif NumBasi == 3
                      DCalMPBELTot(MPBEL,1:3) = DCalMPBEL(:,:);
                  elseif NumBasi == 4
                      DCalMPBELTot(MPBEL,1:4) = DCalMPBEL(:,:);
                  elseif NumBasi == 5
                      DCalMPBELTot(MPBEL,1:5) = DCalMPBEL(:,:);
                  elseif NumBasi == 6
                      DCalMPBELTot(MPBEL,1:6) = DCalMPBEL(:,:);
                  end
               end
               MPBEL = MPBEL+1;
               text = ['Calibration of node MultiPoint Borehole Extensometer Link number ' nNodo ' downloaded correctly'];
               fprintf(fileID,fmt,text);
           end 

    elseif idNodo(a,1) == 8 % Analog Link
           %% Importazione dei dati di calibrazione per Analog Link 
           if strcmp(NodoAnalogLink(AL,3),'mV/V')
              comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                  IDcentralina ''' and ToolNameID = ''' ...
                  DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(NodoAnalogLink(a,3),'4-20 mA')
              comando = ['select Cal0, Cal1, Cal2 from CalibrationView where UnitName = ''' ...
                  IDcentralina ''' and ToolNameID = ''' ...
                  DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(NodoAnalogLink(a,3),'Hz')
                  comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                      IDcentralina ''' and ToolNameID = ''' ...
                      DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalAL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Analog Link
           [rr,cc] = size(DCalAL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Analog Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesAL = 0;
           else
               if strcmp(NodoAnalogLink(AL,3),'mV/V')
                   DCalALTot(AL,1:2) = DCalAL(:,:);
               elseif strcmp(NodoAnalogLink(AL,3),'4-20 mA')
                   DCalALTot(AL,1:3) = DCalAL(:,:);
               elseif strcmp(NodoAnalogLink(AL,3),'Hz')
                   DCalALTot(AL,1:2) = DCalAL(:,:);
               end
                AL = AL+1;
                text = ['Calibration of node Analog Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end 

    elseif idNodo(a,1) == 36 % Crack Link
           %% Importazione dei dati di calibrazione per Crack Link 
           if strcmp(NodoCrackLink(CrL,3),'mV/V')
               comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(NodoCrackLink(CrL,3),'Hz')||strcmp(NodoCrackLink(CrL,3),'Hz mm')
               comando = ['select Cal0, Cal1, Cal2 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalCrL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo Crack Link
           [rr,cc] = size(DCalCrL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Crack Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesCrL = 0;
           else
               if strcmp(NodoCrackLink(CrL,3),'mV/V')
                   DCalCrLTot(CrL,1:2) = DCalCrL(:,:);
               elseif strcmp(NodoCrackLink(CrL,3),'Hz')||strcmp(NodoCrackLink(CrL,3),'Hz mm')
                   DCalCrLTot(CrL,1:3) = DCalCrL(:,:);
               end
                CrL = CrL+1;
                text = ['Calibration of node Crack Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end 
           
    elseif idNodo(a,1) == 37 % 3D Crack Link
           %% Importazione dei dati di calibrazione per 3D Crack Link 
           if strcmp(Nodo3DCrackLink(CrL3D,3),'mV/V')
               comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           elseif strcmp(Nodo3DCrackLink(CrL3D,3),'Hz') || strcmp(Nodo3DCrackLink(CrL3D,3),'4-20 mA')
               comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5, Cal6, Cal7, Cal8 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCal3DCrL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo 3D Crack Link
           [rr,cc] = size(DCal3DCrL);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node 3D Crack Link number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yes3DCrL = 0;
           else
               if strcmp(Nodo3DCrackLink(CrL3D,3),'mV/V')
                   DCal3DCrLTot(CrL3D,1:6) = DCal3DCrL(:,:);
               elseif strcmp(Nodo3DCrackLink(CrL3D,3),'Hz') || strcmp(Nodo3DCrackLink(CrL3D,3),'4-20 mA')
                   DCal3DCrLTot(CrL3D,1:9) = DCal3DCrL(:,:);
               end
                CrL3D = CrL3D+1;
                text = ['Calibration of node 3D Crack Link number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end
           
    elseif idNodo(a,1) == 51 % 2D Crack Link
        %% Importazione dei dati di calibrazione per 2D Crack Link
        if strcmp(Nodo2DCrackLink(CrL2D,3),'Hz') || strcmp(Nodo3DCrackLink(CrL3D,3),'4-20 mA')
            comando = ['select Cal0, Cal1, Cal2, Cal3, Cal4, Cal5 from CalibrationView where UnitName = ''' ...
                IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
        elseif strcmp(Nodo2DCrackLink(CrL2D,3),'mV/V')
            comando = ['select Cal0, Cal1, Cal2, Cal3 from CalibrationView where UnitName = ''' ...
                IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
        end
        curs = exec(conn,comando);
        curs = fetch(curs);
        DCal2DCrL = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo 2D Crack Link
        [rr,cc] = size(DCal2DCrL);
        if rr==1 && cc==1
            text = ['Warning: there was an error reading node 2D Crack Link number ' nNodo ''];
            fprintf(fileID,fmt,text);
            yes2DCrL = 0;
        else
            if strcmp(Nodo2DCrackLink(CrL2D,3),'Hz') || strcmp(Nodo3DCrackLink(CrL3D,3),'4-20 mA')
                DCal2DCrLTot(CrL2D,1:6) = DCal2DCrL(:,:);
            elseif strcmp(Nodo2DCrackLink(CrL2D,3),'mV/V')
                DCal2DCrLTot(CrL2D,1:4) = DCal2DCrL(:,:);
            end
            CrL2D = CrL2D+1;
            text = ['Calibration of node 2D Crack Link number ' nNodo ' downloaded correctly'];
            fprintf(fileID,fmt,text);
        end
        
    elseif idNodo(a,1) == 47 % Stress Meter
           %% Importazione dei dati di calibrazione per Stress Meter
           if strcmp(NodoStressMeter(SM,3),'Hz')
               comando = ['select Cal0, Cal1 from CalibrationView where UnitName = ''' ...
                    IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeNum = ''' nNodo ''' '];
           end
           curs = exec(conn,comando);
           curs = fetch(curs);
           DCalSM = curs.Data; % matrice che raccoglie i dati del nodo numero 'a' di tipo 3D Crack Link
           [rr,cc] = size(DCalSM);
           if rr==1 && cc==1
              text = ['Warning: there was an error reading node Stress Meter number ' nNodo ''];
              fprintf(fileID,fmt,text);
              yesSM = 0;
           else
               if strcmp(NodoStressMeter(SM,3),'Hz')
                   DCalSMTot(SM,1) = DCalSM(:,1);
               end
                SM = SM+1;
                text = ['Calibration of node Stress Meter number ' nNodo ' downloaded correctly'];
                fprintf(fileID,fmt,text);
           end
           
    elseif idNodo(a,1) == 9 % Ancora
           text = 'Anchor';
           fprintf(fileID,fmt,text);         
    end
end

text = 'NuovaConversione function started';
fprintf(fileID,fmt,text);

% Nuova conversione da fare per il nuovo database
[DCalTuLTot,DCalRLTot,DCalTLHTot,DCalTLHRHTot,DCalPCLTot,DCalPCLHRTot,...
    DCalPLTot,DCalLLTot,DCalELTot,DCalEL3DTot,DCalWELTot,DCalMPBELTot,DCalALTot,...
    DCalCrLTot,DCal3DCrLTot,DCal2DCrLTot] = NuovaConversione(DCalTuLTot,yesTuL,DCalRLTot,yesRL,...
    DCalTLHTot,yesTLH,DCalTLHRHTot,yesTLHRH,DCalPCLTot,yesPCL,DCalPCLHRTot,yesPCLHR,...
    DCalPLTot,yesPL,DCalLLTot,yesLL,DCalELTot,yesEL,DCalEL3DTot,yes3DEL,DCalWELTot,yesWEL,...
    DCalMPBELTot,yesMPBEL,DCalALTot,yesAL,DCalCrLTot,yesCrL,DCal3DCrLTot,yes3DCrL,...
    DCal2DCrLTot,yes2DCrL,FileName);

%% Chiudo Testo
text = 'NuovaConversione & letturaCal functions ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end
