MATLAB: Inserting a 1000 separator

1000 separatorformattext;

Hi, I am building a table in which I need to insert numbers with a comman 1000 separator and two decimal points. For example: A=11201453.21 % should be A=1,1201,453.21 Any hint about how to do it? Best,

Best Answer

Here's a function I've used:
%=====================================================================
% Takes a number and inserts commas for the thousands separators.
function [commaFormattedString] = CommaFormat(value)
% Split into integer part and fractional part.
[integerPart, decimalPart]=strtok(num2str(value),'.');
% Reverse the integer-part string.
integerPart=integerPart(end:-1:1);
% Insert commas every third entry.
integerPart=[sscanf(integerPart,'%c',[3,inf])' ...
repmat(',',ceil(length(integerPart)/3),1)]';
integerPart=integerPart(:)';
% Strip off any trailing commas.
integerPart=deblank(integerPart(1:(end-1)));
% Piece the integer part and fractional part back together again.
commaFormattedString = [integerPart(end:-1:1) decimalPart];
return; % CommaFormat