How do I format tick labels before R2016b?

343 ビュー (過去 30 日間)
I have an axis with tick labels and I want them all to have 3.1 format. For example, I do a plot (1:5) and I change my tick labels using the command
set(gca, 'XTick', [1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0])
However, they appear as:
[1 1.5 2 2.5 3 3.5 4 4.5 5]
I would like to get them to appear as I typed them in the above command.
Also, I want to know if it is possible to specify the format of the plot axes tick label which is automatically placed by MATLAB.

採用された回答

MathWorks Support Team
MathWorks Support Team 2021 年 2 月 17 日
編集済み: MathWorks Support Team 2021 年 2 月 17 日
For MATLAB R2015b and newer versions:
This functionality was added to MATLAB graphics system in R2015b. The following code shows the simplest way to achieve this:
plot(1:10);
ax = gca;
ax.XAxis.TickLabelFormat = '%,.1f';
For more information on individual axis customizations, please refer to the following blog:
For MATLAB R2015a and older versions:
You can use one of the following methods to format your tick labels:
1. Use the SPRINTF function to format your tick labels using the format of your choice then set the 'XTickLabel' or 'YTickLabel' property of the axis to use those strings as tick labels.
An example which demonstrates this is:
x=[1 1.53 4];
y=[1 2 3];
plot(x,y)
set(gca,'XTick',x)
set(gca,'XTickLabel',sprintf('%3.4f|',x))
set(gca,'YTick',y)
set(gca,'YTickLabel',sprintf('%+1.2f|',y))
For more information on the available formats, consult the documentation for the SPRINTF function. Also, consult the documentation for the XTickLabel in the 'Axes properties' section.
NOTE: for MATLAB R2014b and later you will have to use new line character instead of the | symbol. For instance, the following command will work for MATLAB R2014b and later.
set(gca,'XTickLabel',sprintf('%3.4f\n',x))
2. Use the STR2MAT function to create a use the tick labels of your choice. An example which demonstrates this is:
x=[1 1.53 4];
y=[1 2 3];
plot(x,y)
set(gca,'XTick',x)
set(gca,'XTickLabel',str2mat('1.00','1.530','2'))
set(gca,'YTick',y)
set(gca,'YTickLabel',str2mat('1.00','2.00','3.00'))
You can change the auto generated tick marks by first getting them using get(gca,'XTickLabel') followed by one of the two methods listed above.
  5 件のコメント
Walter Roberson
Walter Roberson 2016 年 7 月 13 日
Sindar:
No, the "format" command has no effect on any aspect of plotting.

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

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2015 年 9 月 20 日
x=[1 1.53 4];
y=[1 2 3];
plot(x,y)
set(gca,'XTick',x)
set(gca,'XTickLabel', cellstr(num2str(x(:), '%3.4f')))
set(gca,'YTick',y)
set(gca,'YTickLabel', cellstr(num2str(y(:), '%+1.2f')) )

Joe M
Joe M 2016 年 5 月 10 日
The newest, most elegant answer to this issue seems to be:
plot(x,y);
ax = gca;
ax.XAxis.TickLabelFormat = '%,.1f';
I think this functionality must have been added sometime since 2013. I'm running the most recent version, R2016a. The previous solutions suffer from the fact that resizing the plot results in the wrong values for the tick marks. Also, this solution works for axes that have exponents, whereas the previous solutions don't.
  1 件のコメント
Mike Garrity
Mike Garrity 2016 年 5 月 10 日
That was introduced in R2015b. There are a lot of other nice features in there. Check out this post on Loren's blog.

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


Sergio Yanez-Pagans
Sergio Yanez-Pagans 2021 年 8 月 21 日
Maybe this could be useful:
https://www.mathworks.com/matlabcentral/fileexchange/97964-linemarks

Community Treasure Hunt

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

Start Hunting!

Translated by