%% LOCAL FUNCTIONS

function [A, B] = alignTablesForAppend(A, B)
aNames = A.Properties.VariableNames;
bNames = B.Properties.VariableNames;
allNames = unique([aNames, bNames], 'stable');

missingA = setdiff(allNames, aNames, 'stable');
for i = 1:numel(missingA)
    v = missingA{i};
    A.(v) = defaultMissingLike(B.(v), height(A));
end

missingB = setdiff(allNames, bNames, 'stable');
for i = 1:numel(missingB)
    v = missingB{i};
    B.(v) = defaultMissingLike(A.(v), height(B));
end

A = A(:, allNames);
B = B(:, allNames);

for i = 1:numel(allNames)
    v = allNames{i};
    if ~strcmp(class(A.(v)), class(B.(v)))
        if isnumeric(A.(v)) && isnumeric(B.(v))
            A.(v) = double(A.(v));
            B.(v) = double(B.(v));
        else
            A.(v) = string(A.(v));
            B.(v) = string(B.(v));
        end
    end
end
end

function col = defaultMissingLike(exampleCol, n)
if isnumeric(exampleCol)
    col = NaN(n, 1);
elseif islogical(exampleCol)
    col = false(n, 1);
elseif isstring(exampleCol)
    col = strings(n, 1);
elseif iscategorical(exampleCol)
    col = categorical(strings(n, 1));
else
    col = strings(n, 1);
end
end