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

조회 수: 2 (최근 30일)
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 CenterFile Exchange에서 Grid 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