Hi all
How can I change the format of the numbers in the x-axis to be like 10,000 instead of 10000?
number of rounds 10000 but I would like to display like 10,000.
How could I change it?
plot(1:Simulation,ooooo(2,:),'k-.');
title('PDR')
xlabel('Rounds');
ylabel('Time (ms)');
grid on

 採用された回答

Star Strider
Star Strider 2022 年 8 月 11 日

0 投票

In the NumericRuler Properties documentation see TickLabelFormat to understand how to put the comma thousands separator in the tick format.

9 件のコメント

Walter Roberson
Walter Roberson 2022 年 8 月 11 日
This is a relatively new feature involving using comma as a format item in the sprintf()-like format string. The comma is not generally recognized in other places that accept sprintf()-like format strings.
NOUF ALHARBI
NOUF ALHARBI 2022 年 8 月 11 日
Hi
I am traing to add TickLabelFormat but still I have error
plot(1:Simulation,ooooo(2,:),'k-.');
title('PDR')
xlabel('Rounds',TickLabelFormat,'%,g');
ylabel('Time (ms)');
grid on
Are there any ideas?
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
plot(1:Simulation,ooooo(2,:),'k-.');
title('PDR')
xlabel('Rounds')
xtickformat('%,g');
ylabel('Time (ms)');
grid on
The xtickformat() call requires R2016b or later. I am not sure when ',' became supported as a format item; R2018b at latest.
NOUF ALHARBI
NOUF ALHARBI 2022 年 8 月 11 日
Many thanks Walter
This code work with me.
But I have problem, I wnat number like this:
1000 2000 3000 ..... 10,000
with your code add comma but with 1,000 2,000 3,000 ... 10,000
How can I change it?
Rgards,
Nouf
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
The %, format modifier only supports comma for every 3 locations.
You would need to construct your own tick labels according to whatever rules you like. For example,
ax = gca;
xticklabels(ax, regexprep(compose('%d', ax.XTicks), '(\d\d)(\d\d\d)$', '$1,$2'));
This particular code will only put in one comma. For example 3141 31415 314159 3141592 would become 3141 31,415 314,159 3141,592
NOUF ALHARBI
NOUF ALHARBI 2022 年 8 月 11 日
There is error:
Unrecognized method, property, or field 'XTicks' for class 'matlab.graphics.axis.Axes'.
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
ax = gca;
xticklabels(ax, regexprep(compose('%d', ax.XTick), '(\d\d)(\d\d\d)$', '$1,$2'));
NOUF ALHARBI
NOUF ALHARBI 2022 年 8 月 11 日
Thank you so much Walter.
Star Strider
Star Strider 2022 年 8 月 11 日
@Walter Roberson — Thank you. (Internet here down for two hours this morning.)

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 8 月 10 日

0 投票

Turn the number into a string. See my attached little utility function.

3 件のコメント

Image Analyst
Image Analyst 2022 年 8 月 11 日
My function handles those numbers perfectly:
v = [3141 31415 314159 3141592]
v = 1×4
3141 31415 314159 3141592
for k = 1 : numel(v)
str = CommaFormat(v(k))
end
str = '3,141'
str = '31,415'
str = '314,159'
str = '3,141,592'
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
The challenge is that the user does not want the comma for 3141 -- only for 10000 or more.
Image Analyst
Image Analyst 2022 年 8 月 11 日
@Walter Roberson oh, okay. In that case you'd have to put in a check for the number of digits being 5 or greater if he wanted to use that function.

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

カテゴリ

ヘルプ センター および File ExchangeAxes Appearance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by