Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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 ???

Risposte (2)

Star Strider
Star Strider il 4 Ago 2015
Modificato: Star Strider il 4 Ago 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 il 4 Ago 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 Commenti
Pankaj Jha
Pankaj Jha il 4 Ago 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 il 5 Ago 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));

Accedi per commentare.

Categorie

Scopri di più su Vector Fields in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by