function value = utValidateValue(variable, value, delimiters)
% UTVALIDATEVALUE Checks whether the VALUE can be assigned to the VARIABLE.
%
% modelpack.utvalidatevalue(variable, value, delimiters)

% Copyright 2000-2023 The MathWorks, Inc.

% Default arguments
if (nargin < 3 || isempty(delimiters)), delimiters = '.({'; end

ID = variable.getID;

if ~isempty(ID)
    % Adjust size if value is a numeric or logical array.
    if isnumeric(value) || islogical(value)
        [~, subs] = modelpack.varnames(variable.Name, delimiters);
        if isempty(subs)
            % Full variable name is used: assign value after adjusting size.
            value = modelpack.utFormatValueToSize(value, ID.getDimensions);
        else
            % An expression is used: assign value directly if assignment is valid.
            try
                eval(['tmp' subs '= value;']);
                value = eval(['tmp' subs]);
            catch
                error(message('SLControllib:modelpack:CannotAssignVariable', variable.Name ));
            end
        end
    else
        error(message('SLControllib:modelpack:NumericArrayArgument', 'VALUE' ));
    end
else
    error(message('SLControllib:modelpack:EmptyIdentifier'));
end

% LocalWords:  utvalidatevalue tmp
