% Funzione che converte i dati in mV/V della cella di carico in dati di 
% forza mediante i valori di calibrazione.
% Il risultato è la matrico DatiLoad

function DatiMultiBase = conv_grezziMPBEL(DatiMPBEL,NumBasi,DCalMPBELTot,FileName)

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

[rA,cA] = size(DatiMPBEL);
DatiMultiBase = zeros(rA,cA);
 
%% Conversione da mV/V a mm
% Spacchetto i parametri di calibrazione
gain = DCalMPBELTot;

% contatori
ii = 1;
num = 1;

while ii <= cA
    if NumBasi == 1
       DatiMultiBase(:,ii) = (1/gain(num,1))*DatiMPBEL(:,ii); % conversione
       ii = ii+1;
       num = num+1;
    elseif NumBasi == 2
           DatiMultiBase(:,ii) = 1/(gain(num,1))*DatiMPBEL(:,ii); % conversione 
           DatiMultiBase(:,ii+1) = 1/(gain(num,2))*DatiMPBEL(:,ii+1); % conversione 
           ii = ii+2;
           num = num+1;
    elseif NumBasi == 3
           DatiMultiBase(:,ii) = 1/(gain(num,1))*DatiMPBEL(:,ii); % conversione 
           DatiMultiBase(:,ii+1) = 1/(gain(num,2))*DatiMPBEL(:,ii+1); % conversione 
           DatiMultiBase(:,ii+2) = 1/(gain(num,3))*DatiMPBEL(:,ii+2); % conversione
           ii = ii+3;
           num = num+1;
    elseif NumBasi == 4
           DatiMultiBase(:,ii) = 1/(gain(num,1))*DatiMPBEL(:,ii); % conversione 
           DatiMultiBase(:,ii+1) = 1/(gain(num,2))*DatiMPBEL(:,ii+1); % conversione 
           DatiMultiBase(:,ii+2) = 1/(gain(num,3))*DatiMPBEL(:,ii+2); % conversione
           DatiMultiBase(:,ii+3) = 1/(gain(num,4))*DatiMPBEL(:,ii+3); % conversione
           ii = ii+4;
           num = num+1;
    elseif NumBasi == 5
           DatiMultiBase(:,ii) = 1/(gain(num,1))*DatiMPBEL(:,ii); % conversione 
           DatiMultiBase(:,ii+1) = 1/(gain(num,2))*DatiMPBEL(:,ii+1); % conversione 
           DatiMultiBase(:,ii+2) = 1/(gain(num,3))*DatiMPBEL(:,ii+2); % conversione
           DatiMultiBase(:,ii+3) = 1/(gain(num,4))*DatiMPBEL(:,ii+3); % conversione
           DatiMultiBase(:,ii+4) = 1/(gain(num,5))*DatiMPBEL(:,ii+4); % conversione
           ii = ii+5;
           num = num+1;
    elseif NumBasi == 6
           DatiMultiBase(:,ii) = 1/(gain(num,1))*DatiMPBEL(:,ii); % conversione 
           DatiMultiBase(:,ii+1) = 1/(gain(num,2))*DatiMPBEL(:,ii+1); % conversione 
           DatiMultiBase(:,ii+2) = 1/(gain(num,3))*DatiMPBEL(:,ii+2); % conversione
           DatiMultiBase(:,ii+3) = 1/(gain(num,4))*DatiMPBEL(:,ii+3); % conversione
           DatiMultiBase(:,ii+4) = 1/(gain(num,5))*DatiMPBEL(:,ii+4); % conversione
           DatiMultiBase(:,ii+5) = 1/(gain(num,6))*DatiMPBEL(:,ii+5); % conversione
           ii = ii+6;
           num = num+1;
    end
end

text = 'Calibrations of MultiPoint Borehole Rod Extensometer defined correctly. conv_grezziMPBEL function closed';
fprintf(fileID,fmt,text);
fclose(fileID);
end