% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% This function will assign two different sets of data to the same variable%
% using the respective times. When data is only found in one of the inputs %
% the other value will be -60140. The first thing it does is to found where%
% the lesser data is in between values in the greater data and then put it %
% in the closer spot. The data with most measures will be the first in the %
% variable, and its time will be the one used (approximation)              %
%                                                                          %
%                                                                          %
% data = function_assignData(data1, data2)                                 %
%  INPUTS:   Name           | Type    | Dimensions | Description           %
%            data1          | measure | nx2        | measures #1           %
%            data2          | measure | mx2        | measures #2           %
%                                                                          %
% OTUPUTS:   Name           | Type    | Dimensions | Description           %
%            data           | measure | nx2        | both measures         %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %

function data = function_assignData(data1, data2)
    
    % This will make temp1 the data with most measures. That way it will be
    % possible to make temp2 adjust to temp1 (always)
    if (size(data1,1) > size(data2,1))
        temp1 = data1;
        temp2 = data2;
    else
        temp1 = data2;
        temp2 = data1;
    end
    
    
    % Creation of data variable (size in 1 equals to the greatest and size
    % in 2 equals to the sum of both data minus 1 (since time is repeated))
    % Notice that any value that is not changed will have -60140 as it
    % value! This is usefull to understand if a given value is exists or
    % not
    data = ones(size(temp1,1), size(temp1,2) + size(temp2,2) - 1) * - 60140;
    % Since the assign cycle (for cycle) will begin in two, then we need to
    % add the first value of temp1. Temp2 is not added because if need be,
    % it will be added in the assign cycle.
    data(1,1:size(temp1,2)) = temp1(1,:);
    
    
    z = 1;
    for a = 2:size(temp1,1)
        % If the signs are different in (a-1) and (a), then the time in
        % temp2 is in the middle of temp1(a-1,:) and temp1(a,:). If this
        % condition is met, there is a need to add temp2 data. Notice that
        % when the temp2 does not exist, the values correspond to -60140
        % Z is the number of measures added. It is necessary to use this
        % variable in case we add all the measures in temp2 and haven't
        % analysed temp1. If this is the case, we will try to access data
        % that does not exist.
        if (z <= size(temp2,1))
            % If temp2 started before temp1, the cycle will not detect a
            % measure whom temp2 is between with, because there is none.
            % Therefore, we need to advance temp2 until after the beggining
            % of temp1.
            while(temp2(z,1) < temp1(a-1,1))
                z = z + 1;
            end
            
            if (sign(temp1(a-1,1) - temp2(z,1)) ~= sign(temp1(a,1) - temp2(z,1)))
                % In this case, the temp2(z,:) is closer to the temp1(a,:),
                % since the difference is minor.
                if(abs(temp1(a-1,1) - temp2(z,1)) > abs(temp1(a,1) - temp2(z,1)))
                    data(  a,size(temp1,2)+1:end) = temp2(z,2:end);
                else
                    data(a-1,size(temp1,2)+1:end) = temp2(z,2:end);
                end
               z = z + 1;
            end
        end
        % This will add temp1 data. Temp2 is added above since it needs a
        % special condition to be met. Temp1 does not need any condition, 
        % as it must be added each iteration, then it happens out of the if
        % cycle.
        data(a,1:size(temp1,2)) = temp1(a,:);
    end
end