function [dvData,dataLabels,numDataVars] = extractDataVars(T,groupVars,dataVars_,tableFlag,dvNotProvided,allowCellT)
%MATLAB Code Generation Library Function
% EXTRACTDATAVARS Extracts data variables for table and matrix input by
% putting each data vector into a cell array. Returns a cell array with the
% data vectors, the labels associated with the data, and the number of data
% variables.


%   Copyright 2021-2022 The MathWorks, Inc.
%#codegen

coder.internal.prefer_const(groupVars,dataVars_,tableFlag,dvNotProvided);
ONE = coder.internal.indexInt(1);

if nargin < 6
    allowCellT = false;
else
    coder.internal.prefer_const(allowCellT);
end
if tableFlag
    if dvNotProvided
        if istimetable(T)
            idx = coder.const(feval('ismember',groupVars,T.Properties.DimensionNames{1}));
            groupVars_ = {groupVars{~idx}};
        else
            groupVars_ = groupVars;
        end
        dataVars = coder.const(feval('setdiff',dataVars_,groupVars_,'stable'));
    else
        dataVars = dataVars_;
    end
    numDataVars = numel(dataVars);
    dataLabels = coder.const(dataVars);
    dvData = cell(1,numDataVars);
    coder.unroll();
    for k=1:numDataVars
        dvData{k} = T.(dataVars{k});
    end
else
    % For matrix case, the number of dataVars is the number of columns
    numDataVars_ = size(T,2);
    
    if (numDataVars_ ~= 0)        
        % Copy columns over to cell array
        if allowCellT && iscell(T) && ~iscellstr(T) %#ok<ISCLSTR>
            % This is the case where we have a cell with one vector or
            % matrix
            t = T{1};
            if isempty(t) && size(t,2) < 2
                dvData = T;
                numDataVars = 1;
            else
                numDataVars = size(t,2);
                dvData = cell(1,numDataVars);
                if coder.internal.isConst(numDataVars)
                    coder.unroll();
                end
                for ii = ONE:numDataVars
                    dvData{ii} = t(:,ii);
                end
            end
        else
            numDataVars = numDataVars_;
            dvData = cell(1,numDataVars);
            if coder.internal.isConst(numDataVars)
                coder.unroll();
            end
            for k=1:numDataVars
                % FIXME dead code?
                if iscell(T)
                    dvData{k} = {T{:,k}};
                else
                    dvData{k} = T(:,k);
                end
            end
        end
        dataLabels = cell(1,numDataVars);
        for ii = ONE:numDataVars
            dataLabels{ii} = ii;
        end
    else
        % Special case where the input is an nx0 vector
        numDataVars = 1;
        dataLabels = {ONE};
        dvData = {T};
    end
end