function s = table2struct(t,varargin)  %#codegen
%TABLE2STRUCT Convert table to structure array.
%   S = TABLE2STRUCT(T) converts the table T to a structure array S.  Each
%   variable of T becomes a field in S.  If T is an M-by-N table, then S is
%   M-by-1 and has N fields.
%
%   S = TABLE2STRUCT(T,'ToScalar',true) converts the table T to a scalar
%   structure S.  Each variable of T becomes a field in S.  If T is an
%   M-by-N table, then S has N fields, each of which has M rows.
%
%   S = TABLE2STRUCT(T,'ToScalar',false) is identical to S = TABLE2STRUCT(T).
%
%   See also STRUCT2TABLE, TABLE2CELL, TABLE.

%   Copyright 2012-2019 The MathWorks, Inc.

if ~isempty(coder.target)
    % codegen, redirect to codegen specific function and return
    s = matlab.internal.coder.table2struct(t, varargin{:});
    return
end

pnames = {'ToScalar'};
dflts =  {    false };
[toScalar] = matlab.internal.datatypes.parseArgs(pnames, dflts, varargin{:});

toScalar = matlab.internal.datatypes.validateLogical(toScalar,'ToScalar');
if toScalar
    s = getVars(t);
else
    s = cell2struct(table2cell(t),matlab.internal.tabular.makeValidVariableNames(t.Properties.VariableNames,'warn'),2);
end