how to retain the label of the tick and add a new label at any specified tick value

2 ビュー (過去 30 日間)
Pankaj Jha
Pankaj Jha 2015 年 8 月 4 日
コメント済み: Kelly Kearney 2015 年 8 月 5 日
close all x = linspace(0,4*pi); y = sin(x); plot(x,y) axis([0 4*pi -1.2 1.2]) % Define y-ticks and their labels.. set(gca,’yTick’,-0.5) set(gca,’yTickLabel’,{’abc’}); %% this labels the y axis. %% now add another label set(gca,’yTick’,0.5) set(gca,’yTickLabel’,{’def’});
The problem is the command to set the new label deletes the previous labels and adds the new one. I want add a new label while retaining the previous labels. how to get that ???

回答 (2 件)

Star Strider
Star Strider 2015 年 8 月 4 日
編集済み: Star Strider 2015 年 8 月 4 日
There is no way to do what you want, even using the hold function. You have to combine the tick labels together in the same call:
x = linspace(0,4*pi);
y = sin(x);
plot(x,y)
axis([0 4*pi -1.2 1.2])
% Define y-ticks and their labels..
set(gca,'yTick',[-0.5 0.5], 'yTickLabel',{'abc', 'def'})

Kelly Kearney
Kelly Kearney 2015 年 8 月 4 日
If you want to keep the original numeric ticks as well, the following will do that:
x = linspace(0,4*pi);
y = sin(x);
plot(x,y);
axis([0 4*pi -1.2 1.2]);
ytick = get(gca, 'ytick');
yticklab = cellstr(num2str(ytick'));
ytick = [ytick -0.5 0.5];
yticklab = [yticklab' 'abc' 'def'];
[ytick,ia] = unique(ytick, 'last');
yticklab = yticklab(ia);
set(gca, 'ytick', ytick, ...
'yticklabel', yticklab);
  2 件のコメント
Pankaj Jha
Pankaj Jha 2015 年 8 月 4 日
Thanks for the reply. I want my
yTick=[0.1 0.2 -0.3 -0.5 0.4];yTickLabel=[ab cd ef gh ij];
and I want to maintain the order in which they are labeled. plz help.
Kelly Kearney
Kelly Kearney 2015 年 8 月 5 日
Not quite sure what you mean by maintain the order... something like this?
x = linspace(0,4*pi);
y = sin(x);
plot(x,y);
axis([0 4*pi -1.2 1.2]);
yTick = [0.1 0.2 -0.3 -0.5 0.4];
yTickLabel = {'ab' 'cd' 'ef' 'gh' 'ij'};
[yTick, isrt] = sort(yTick);
set(gca, 'ytick', yTick, 'yticklabel', yTickLabel(isrt));

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

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by