function [vi,changed1,changed2] = combineVariableInfo(vi1,vi2)
%combineVariableInfo Combine two VariableInfo tables.

% This operation affects only the Range variable.
% Continuous ranges are expanded to give the overall min/max.
% Categorical ranges are the union of the original ranges.

%   Copyright 2016-2017 The MathWorks, Inc.

vi = vi1;
changed1 = false;
changed2 = false;
nvars = size(vi1,1);
for j=1:nvars
    r1 = vi1.Range{j};
    r2 = vi2.Range{j};
    if vi1.IsCategorical(j)
        if ischar(r1)
            r = sortrows(union(r1,r2,'rows'));
        else
            r = sort(union(r1,r2));
        end
        if ~isequal(r,r1)
            changed1 = true;
        end
        if ~isequal(r,r2)
            changed2 = true;
        end
    else
        if isempty(r1)
            r = r2;
        elseif isempty(r2)
            r = r1;
        else
            r = [min(r1(1),r2(1)), max(r1(2),r2(2))];
        end
    end
    vi.Range{j} = r;
end
end
