% funzione che definisce i dati per la cella di carico, in particolare
% tempo (TimeLL), batteria (BattLL) e ADC (ADCLoad)

function [TimeLL,ADCLoad,ErrLoadLink] = defDatiLL(DatiLoadLink,ErrLoadLink,...
    rLL,Ndatidespike,FileName)

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

[r,c]=size(DatiLoadLink);
Ncorr = 0;
% Elimino gli eventuali Not a Number
for a = 2:r
    for b = 1:c
        check = isnan(DatiLoadLink(a,b));
        if check == 1
            DatiLoadLink(a,b) = DatiLoadLink(a-1,b);
            ErrLoadLink(a,b) = 1;
            Ncorr = Ncorr+1;
        end
    end
end

text = ['' num2str(Ncorr) ' NaN of Load Link corrected by defDatiLL function'];
fprintf(fileID,fmt,text);

TimeLL = DatiLoadLink(:,1); % data
ADCLoad = DatiLoadLink(:,2:end); % dati ADC Analog Link
for s = 1:rLL
    % despiking
    ADCLoad(1:end-1,s) = filloutliers(ADCLoad(1:end-1,s),'linear','movmedian',Ndatidespike);
end

text = 'Data of Load Link defined correctly. defDatiLL function closed';
fprintf(fileID,fmt,text);
fclose(fileID);

end