function [x,y,vnames,yname] = getVariableFromTable(x,y,vnames,yname)
% Extract variable y (response or weights) from table if necessary

%   Copyright 2017-2019 The MathWorks, Inc

if istall(y) || isempty(y)
    return
end

% If x is a table and y is a variable name, separate them
if (isstring(y) && isscalar(y)) || (ischar(y) && isrow(y))
    if gather(ismember(y,vnames))
        yname = y;
        sidx = substruct('.',yname);
        y = subsref(x,sidx);
        cols = ~strcmp(yname,vnames);
        sidx = substruct('()',{':',cols});
        x = subsref(x,sidx);
        vnames = vnames(cols);
    else
        % y is neither a tall variable nor the name of one
        error(message('stats:classreg:regr:FitObject:BadResponseName'));
    end
else
    % y is neither a tall variable nor the name of one
    error(message('stats:classreg:regr:FitObject:AllTall'));
end
end