function IA = standardizeMissing(A,indicators,varargin)
%MATLAB Code Generation Library Function

%   Limitations:
%   Requires variable-sizing
%   Sparse inputs are not supported

%   Copyright 2018-2023 The MathWorks,Inc.
%#codegen

coder.internal.assert(isnumeric(A) || islogical(A) || ischar(A) || ...
    isa(A,'table') || isa(A,'duration') || isa(A,'categorical') || ...
    iscellstr(A) || isa(A,'datetime') || isa(A,'timetable'), ...
    'MATLAB:standardizeMissing:InvalidInputsCodegen'); %#ok<ISCLSTR>
coder.internal.assert(~issparse(A), 'Coder:toolbox:SparseNotSupported',mfilename);

ISTABLEORTIMETABLE = isa(A,'table') || isa(A,'timetable');

if numel(varargin)>0
    % Parse DataVariables
    parms = {'DataVariables','ReplaceValues'};
    poptions = struct( ...
        'CaseSensitivity',false, ...
        'PartialMatching','unique', ...
        'StructExpand',false, ...
        'IgnoreNulls',false);
    pstruct = coder.internal.parseParameterInputs( ...
        parms,poptions,varargin{:});
    coder.internal.assert(ISTABLEORTIMETABLE || pstruct.DataVariables == 0,'MATLAB:standardizeMissing:DataVariablesArray');
    dataVars = coder.internal.getParameterValue( ...
        pstruct.DataVariables,1:width(A),varargin{:});
    coder.internal.assert(ISTABLEORTIMETABLE || pstruct.ReplaceValues == 0,'MATLAB:standardizeMissing:DataVariablesArray');
    replaceValues = coder.internal.getParameterValue( ...
        pstruct.ReplaceValues,true,varargin{:});
    coder.internal.assert(coder.internal.isConst(replaceValues), ...
        'Coder:toolbox:OptionInputsMustBeConstant','ReplaceValues');
    validateattributes(replaceValues,{'logical'}, ...
        {'scalar'},mfilename,'ReplaceValues',pstruct.ReplaceValues + 2);
else
    if ISTABLEORTIMETABLE
        dataVars = coder.const(coder.internal.indexInt(1):coder.internal.indexInt(width(A)));
        replaceValues = true;
    end
end

if ISTABLEORTIMETABLE

    IATmp = coder.internal.applyToTableVariables(A, ...
        @(x) ismissingKernel(x,indicators,true,true), dataVars, true,  ...
        true, replaceValues, 'standardized', ...
        'MATLAB:standardizeMissing:DataVariablesTableSubscript');
    if istimetable(A)
        IA = table2timetableWithDimNames(IATmp, A.Properties.RowTimes, ...
            A.Properties.DimensionNames);
    else
        IA = IATmp;
    end
else
    IA = ismissingKernel(A,indicators,true,false);
end