function args = getMessageIDArgs(msgId, errorMsg)
% given a msgID and errorMsg this function returns the
% arguments from the errorMsg in the order of their indices in msgId string in the xml file
%
% For example if:
% msgId = EMLRT:runTime:OutOfBoundRangeNamed
% errorMsg =
% Index exceeds array dimensions. Index value -49 exceeds valid range [1-5] for array 'a'.
% Error in '<a href=""matlab:hilite_system('mUndecidedDueToRunTimeError/MATLAB Function')"">mUndecidedDueToRunTimeError/MATLAB Function</a>'
% (<a href=""matlab:sf('Open', sfprivate('block2chart', 'mUndecidedDueToRunTimeError/MATLAB Function'), 2, -2);"">line 3</a>)
%		y = a(u);
% then getMessageIDArgs(msgID, errorMsg) = {-49, 1, 5, 'a'}.

args = {};
if isempty(msgId) || isempty(errorMsg)
    return;
end
try
    % get the string for the msgID from the xml file
    msg = slsvInternal('slsvGetEnStringFromCatalog', msgId);
    argPos = regexp(msg, '{(\d+)}', 'tokens');
    special_chars = {'[', ']', '(', ')', '$', '|'};
    for c = special_chars
        msg = regexprep(msg, c{1}, ['\\' c{1}]);
    end
    msg = regexprep(regexprep(regexprep(msg, '{\d+}', '{.*?}'), '{', '('), '}', ')');    
    t = regexp(errorMsg, msg, 'tokens');
    if ~isempty(t)
        for i=1:numel(t{1})
            % argument index starts from 0 in xml file, so we add +1 to start the index from 1 in MATLAB
            j = str2double(argPos{i}{1})+1;
            k = str2double(t{1}{i});
            if ~isnan(k)
                args{j} = k;
            else
                args{j} = t{1}{i};
            end
        end
    else
        args = {};
    end
catch MEx
    % Ahh! things did not go well for us so we set args list to {}.
    args = {};
end
end