I would like to turn a number into a string using a comma as a thousands separator. Something like:
x = 120501231.21;
str = sprintf(\'%0.0f\', x);
One way to format numbers with thousands separators is to call the Java locale-aware formatter. The "formatting numbers" article at the "Undocumented Matlab" blog explains how to do this:
>> nf = java.text.DecimalFormat;
>> str = char(nf.format(1234567.890123))
str =
1,234,567.89
where the char(…)
converts the Java string to a Matlab string.
voilà!