how to set 1000 separator for large numbers in GUI MATLAB

Hello! I am trying to find a way to set 1000 separator for the large numbers displayed in my GUI. For example: I would like the number 100000 to be displayed in my GUI like 100.000 or 100,000.
How could I do it? Thank you, in advance!

2 件のコメント

Juan Saenz-Diez
Juan Saenz-Diez 2020 年 5 月 18 日
Hello! I wonder if this is what Natalia was asking, and whether she got her answer. What I would like to do is what I think Natalia is asking: how to get Matlab to display "normal" numbers with 1000 separators automatically, not in our own code output. Like doing "format bank" but adding the ',' separator to the thousands (as any banking application would do, which is the goal). Anyway, that is what I am looking for. Thanks for helping!
Walter Roberson
Walter Roberson 2026 年 3 月 19 日
There is no way to get MATLAB to display numbers with 1000 separators automatically. You need to convert the numbers to strings and post-process the numbers, or you need to use Java techniques.

サインインしてコメントする。

回答 (5 件)

Jan
Jan 2011 年 5 月 24 日

1 投票

Without Java:
S = sprintf('%.16g', pi * 1e12); % [EDITED: was '%16']
T(1:length(S)) = char(0);
T(strfind(S, '.') - 4:-3:1) = char(39); % [EDITED: -3 -> -4]
S = [S; T];
S = reshape(S(S ~= 0), 1, []);
>> 3'141'592'653'589.793

4 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 5 月 24 日
I get:
>> S = sprintf('%16g', pi * 1e12)
S =
3.14159e+012
Also, 31'....'89.793 doesn't look correct.
Jan
Jan 2011 年 5 月 24 日
@Oleg: Thanks. Fixed typos.
adams13
adams13 2023 年 3 月 3 日
編集済み: adams13 2023 年 3 月 8 日
There is a small bug in this solution. For the integer value, it will not add any thousend separators.
strfind(S, '.')
will produce an empty array.
One can replace the line by
dotPos = strfind(S, '.');
if isempty(dotPos)
dotPos = length(S) + 1;
end
T(dotPos - 4:-3:1) = char(39); % [EDITED: -3 -> -4]
or even better
T(find([S '.'] == '.', 1) - 4:-3:1) = char(39); % [EDITED: -3 -> -4, strfind -> find([... '.'],1)]
Otherwise the solution is charming ;-)
Jan
Jan 2026 年 3 月 18 日
An updated version:
function S = NumWithSep(N, FSpec, Sep)
% N: Number to display
% FSpec: Format specifier for SPRINTF
% SEP: Char used as separator
S = sprintf(FSpec, N);
Fin = strfind(S, '.');
if isempty(Fin)
Fin = length(S) + 1;
end
Ini = find(isstrprop(S, 'digit'), 1);
S(2, Fin - 4:-3:Ini) = Sep;
S = S(S ~= char(0)).';
end

サインインしてコメントする。

Stephen23
Stephen23 2026 年 3 月 19 日
編集済み: Stephen23 2026 年 3 月 19 日

1 投票

No FLIPLR required:
F = @(t) regexprep(t,'(?<!(\.|[eE][-+]?)\d*)\d{1,3}(?=(\d{3})+(e|E|\.|\>))', '$&,');
And tested:
F('1 12 123 1234 12345 123456 1234567 12345678 123456789') % integer
ans = '1 12 123 1,234 12,345 123,456 1,234,567 12,345,678 123,456,789'
F('0.123456789 123456789.123456789') % decimal point
ans = '0.123456789 123,456,789.123456789'
F("123456789 123456789.123456789") % string
ans = "123,456,789 123,456,789.123456789"
F('0 NaN Inf') % special cases
ans = '0 NaN Inf'
F('-1 +12 -123 +1234 -12345 +123456 -1234567 +12345678 -123456789') % sign
ans = '-1 +12 -123 +1,234 -12,345 +123,456 -1,234,567 +12,345,678 -123,456,789'
F('1e12345 123456789e-12345 123456789.123456789e-12345') % e-notation
ans = '1e12345 123,456,789e-12345 123,456,789.123456789e-12345'
Oleg Komarov
Oleg Komarov 2011 年 5 月 24 日

0 投票

Another alternative w/o java:
n = pi * 1e12;
c = fix(log10(n)+1);
dec = 3;
fmt = [repmat('%c',1,mod(c,dec)) repmat('''%c%c%c',1,fix(c/dec)) '%s'];
sprintf(fmt, sprintf('%.3f',n))
ans =
3'141'592'653'589.793
Matt Fig
Matt Fig 2011 年 5 月 24 日

0 投票

Another alternative.
H = '123456.09'; % A string number.
S = regexp(H,'\.','split');
S{1} = fliplr(regexprep(fliplr(S{1}),'\d{3}(?=\d)', '$0,'));
H2 = [S{1},'.',S{2}]
Note that if H will never have a decimal amount, this becomes a simple one-liner. In this case,
H = '123456'; % A string number with no decimal point.
H2 = fliplr(regexprep(fliplr(H),'\d{3}(?=\d)', '$0,'))

3 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 5 月 24 日
One liner:
fliplr(regexprep(fliplr(H),'(\d+\.)?\d{3}(?=\d)', '$0,'))
Matt Fig
Matt Fig 2011 年 5 月 25 日
Nice Oleg, I looked for that but couldn't find it. Great work!
That lazy quantifier is a miracle worker, I need to spend more time with it.
Dominique
Dominique 2023 年 8 月 29 日
編集済み: Dominique 2023 年 8 月 29 日
It seems to me that this method works ideally when H is char array.
If the numericals were stored in a string array, then mind converting string to char .
example: table height is 62500
H = string(height(my_table)); % string var
H2 = fliplr(regexprep(fliplr(H),'\d{3}(?=\d)', '$0,')) % returns: 625,00 -> misleading
H = char(H) % char var
H2 = fliplr(regexprep(fliplr(H),'\d{3}(?=\d)', '$0,')) % returns: 62,500 -> expected
H2 = fliplr(regexprep(fliplr(H),'\d{3}(?=\d)', '$0 ')) % returns: 62 500 -> blank space separator
% ! Note:
H = char(height(my_table)); % may lead you to trouble

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2011 年 5 月 24 日

編集済み:

2026 年 3 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by