function t = removevars(t, vars)
%REMOVEVARS Delete variables from a codistributed table or timetable.
%   T2 = REMOVEVARS(T1, VARS)
%   
%   See also TABLE, CODISTRIBUTED.
%   


%   Copyright 2018-2020 The MathWorks, Inc.

if ~istable(t) && ~istimetable(t)
    error(message('parallel:array:UnsupportedFunction', upper(mfilename), underlyingType(t)));
end
if nargin<2
    % No vars. We're done.
    return
end

% t must be codistributed, vars must not
distributedutil.CodistParser.verifyNonCodistributedInputs({t, vars});
vars = distributedutil.CodistParser.gatherIfCodistributed(vars);
if ~isa(t, 'codistributed')
    % Not distributed. Run replicated.
    t = removevars(t, vars);
    return;
end

% Call the in-memory version to do input checking and get the desired
% output table template.
inProto = getTableTemplate(t);
outProto = distributedutil.validateSyntax(@removevars, {t, vars});


% Let subsref do the real work. We can work out which variables are being
% kept using their names, which are guaranteed unique.
toKeep = ismember(inProto.Properties.VariableNames, outProto.Properties.VariableNames);
t = subsref(t, substruct('()', {':',toKeep}));

