%# 1. create your formated string x = 12345678; str = sprintf('%d',x)
%# 2. use regexprep to add commas %# flip the string to start counting from the back %# and make use of the fact that Matlab regexp don't overlap %# The three parts of the regex are %# (\d+.)? - looks for any number of digits followed by a dot %# before starting the match (or nothing at all) %# (\d{3}) - a packet of three digits that we want to match %# (?=\S+) - requires that theres at least one non-whitespace character %# after the match to avoid results like ",123.00"
str = fliplr(regexprep(fliplr(str), '(\d+.)?(\d{3})(?=\S+)', '$1$2,'))