MATLAB Answers

Incorrect values using ytickformat

11 views (last 30 days)
Dear all,
I would like to reduce the number of dcp for my right y axis. However, when using ytickformat, instead of reducing the number of dcp, I get a totally different set of y values. Below I have included two images to illustrate my problem.
A) I would like to change 4.54752 to just 4.54.
B) But when I apply attempt to do so I get this.
My code is included below.
x_decade = 1:10;
figure(2);
% Go to left y axis
yyaxis left;
p1 = plot(x_decade,sum_ET10(1,:),'-k',x_decade,sum_P10(1,:),'-b', x_decade,sum_Runoff10(1,:),'-r', x_decade,sum_dSdt10(1,:),'-c', x_decade,sum_C10(1,:),'-g');
p1(1).LineWidth = 2;
p1(2).LineWidth = 2;
p1(3).LineWidth = 2;
p1(4).LineWidth = 2;
p1(5).LineWidth = 2;
yt = get(gca,'YTick');
ylabel('km^3/year')
% Go to right y axis
yyaxis right;
% Evenly space the right y limits according to left y axis
ytr = linspace(min(ylim), max(ylim), numel(yt));
% Convert the right y axis into mm/month
y1 = yt/2199000*1000000;
set(gca, 'YTick',ytr, 'YTickLabel',y1);
ytickformat('%.2f');
ylabel('mm/year');
% Rename x axis variables
set(gca, 'xtick', [1;2;3;4;5;6;7;8;9;10]);
set(gca,'xticklabels',['00-09';'10-19';'20-29';'30-39';'40-49';'50-59';'60-69';'70-79';'80-89';'90-99']);
Any help is greatly appreciated. Thank you.

  0 Comments

Sign in to comment.

Accepted Answer

Chidvi Modala
Chidvi Modala on 16 Apr 2020
The ytickformat function controls how tick label strings are automatically calculated based on tick values, but only if the user has not specified their own tick labels.
When the YTickLabel or TickLabels property is set, it overrides automatic generation of the tick labels. If the user has manually specified tick labels, we display exactly what the user provided with no manipulation. The TickLabelFormat has no impact in this case.
The YTick/TickValues are only used to generate tick labels when the YTickLabel property is set to 'auto'. Otherwise, the user provided labels are used directly.
I think the confusion here is that you are setting the YTickLabel property to a numeric vector. When you set the YTickLabel property to a numeric vector, the values are immediately converted into character vectors. If you query the value of the YTickLabel property back again, you will see that the numeric values have been replaced by a character matrix. Once this conversion is done, MATLAB no longer treats them as numbers, they are now labels to be used literally. The TickLabelFormat has no impact on how this conversion is being done.
What you can to do is either set the YTick values and use the TickLabelFormat (or ytickformat) to control how the tick values are converted into labels, or you can generate labels for yourselves using something like 'sprintf' or 'compose', which can take a numeric vector and a format string. This will produce labels formatted to the user's specification, then you can set the YTickLabel property with those pre-formatted labels.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!