function [fclosest, index] = closest_value(val, data)

    div_val = fix(val / 90);
    
    if length(data) <= 25
        switch div_val
            case 1   
                val = 180 - val; %ex. 120: 180-120 = 60; 170: 180-170 = 10;
            case 2
                val = 180 - val; %ex 190: 180-190 = -10; 250: 180-250 = -70;
            case 3
                val = val - 360; %ex 190: 280-360 = -80; 250: 340-360 = -20;
            otherwise
                val = val;
        end  
        
        if val > 80
            val = 79;
        elseif val < -80
            val = -79; 
        end
    else
        if val < -45
            val = -45;
        elseif val > 230
            val = 230;
        end
    end
    
    % Avoid making error because there is no data for 90 and -90
 
    
    [N,bin]=histc(val,data);
    
    index=bin+1;
    
    if abs(val-data(bin))<abs(val-data(bin+1))
        fclosest=data(bin)
        index=bin;
    else
        fclosest=data(index)
    end

