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

1 view (last 30 days)
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 ???

Answers (2)

Star Strider
Star Strider on 4 Aug 2015
Edited: Star Strider on 4 Aug 2015
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 on 4 Aug 2015
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 Comments
Pankaj Jha
Pankaj Jha on 4 Aug 2015
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 on 5 Aug 2015
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));

Sign in to comment.

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!