How to show full numbers as labels in the Y-axis of a plot

28 次查看(过去 30 天)
I'm creating plots where the Y-axis can range from 0 to 40,000. If I let Matlab automatically format the plot, it will use something like ['0' '1' '2' '3' '4'] as the Y-axis labels with a "x 10^4" printed above the upper left corner of the plot. What is the easiest way to write code to make Matlab display Y-axis labels like ['0' '10,000' '20,000' '30,000' '40,000']?
I've noticed that I can do something like
y_labels = get(gca, 'YTick')
set(gca, 'YTickLabel, y_labels)
This will give me the full numbers, but it won't put commas in the numbers. I can also take the plot, dock it in the Plot Tools editor, click the "Y Axis" tab, and click the "Ticks ..." button which brings up the axis editor window. From there if I click "Manual" under "Y Tick Labels", and then click apply, it will give me the full numbers in the format I want, but if I add the equivalent code
set(gca, 'YTickLabelMode', 'Manual')
to my program just before printing the plot, it just removes the "x 10^4" leaving single digit integers as the Y-axis labels. (Putting this line of code earlier in the program before the plot is finished produces other odd Y-axis labelings.)
I figure there should be an easy way to do it otherwise I will have to convert the numbers to strings, and then write my own function to add commas to the strings.

采纳的回答

Jarrod Rivituso
Jarrod Rivituso 2011-4-13
If you decide to go the "write your own function" route, have a look at this technical solution:
I did a quick modification to the function shown there so it supports vector inputs:
function out = ThousandSep(in)
import java.text.*
v = DecimalFormat;
for i = 1:numel(in)
out{i} = char(v.format(in(i)));
end
Now, it's pretty easy to change my ylabels. Example...
>> plot(1:1000:100000);
>> tickVals = get(gca,'YTick');
>> newTickLabels = ThousandSep(tickVals);
>> set(gca,'YTickLabel',newTickLabels)
  1 个评论
Scott
Scott 2011-5-17
Since no one else has any suggestions on this, I guess this is the best solution. I did something similar to convert an array of data, and it works just fine.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by