MATLAB: How to insert the comma marker for thousands into large numbers while printing to file or Command Line in MATLAB 7.7 (R2008b)

MATLABthousand

I would like my numbers to appear with comma markers for thousands. For instance, I would like to display the number 1200387 as 1,200,387

Best Answer

To add comma separators between groups of three numbers to indicate the thousands place of a large number, save the following code to a file called "ThousandSep.m":
function out = ThousandSep(in)
%THOUSANDSEP adds thousands Separators to a 1x1 array.
% Example:
% ThousandSep(1234567)
import java.text.*
v = DecimalFormat;
out = char(v.format(in));
Then you can use the ThousandSep function (as defined above), to obtain a string with comma separated digits using the following line of code:
ThousandSep(123456778)
The resulting answer is:
ans =
123,456,778