function varName = utCheckVariableSubsref(varID, subsName)
% UTCHECKVARIABLESUBSREF Checks whether the subscripted variable (port, state,
% parameter) name is compatible with the variable identifier object.

% Copyright 2000-2023 The MathWorks, Inc.

if isa(varID, 'modelpack.ParameterID')
    varName = LocalGetVariableName(varID, subsName, []);
elseif isa (varID, 'modelpack.StateID')
    varName = LocalGetVariableName(varID, subsName, '(');
elseif isa(varID, 'modelpack.PortID')
    varName = LocalGetVariableName(varID, subsName, '(');
else
    error(message('SLControllib:modelpack:InvalidInputArguments' ));
end

end

function varName = LocalGetVariableName(varID, subsName, delimiters)
% Get variable name
if isempty(subsName)
    varName = varID.getName;
else
    [name, subs] = modelpack.varnames(subsName, delimiters);

    if strcmp(name, varID.getName)
        varName = [varID.getName subs];
    else
        error(message('SLControllib:slcontrol:VariableNotFound', subsName) );
    end
end
end