% funzione che definisce i dati per la Cella di Carico, in particolare
% tempo (TimeLL) e Dato (Load)

function [TimeLL,Load,ErrLoadLink] = defDatiLL(DatiLoadLink,ErrLoadLink,...
    Ndatidespike,rLL,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 = 2:c
        check = isnan(DatiLoadLink(a,b));
        if check == 1
            DatiLoadLink(a,b) = DatiLoadLink(a-1,b);
            ErrLoadLink(a,b-1) = 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
Load = DatiLoadLink(:,2:end); % dati Load Link
Num_Dati = Ndatidespike; % numero di dati per il despike
if Num_Dati > r
    Num_Dati = r;
end
for i = 1:rLL
    % despiking accelerometri
    Load(1:end-1,i) = filloutliers(Load(1:end-1,i),'linear','movmedian',Num_Dati);      
end

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