user4861528
user4861528

Reputation: 467

how to add xtick and xtick label in matlab without changing the rest of the automatic ticks and ticklabels

I want to mark some special point on the x-axis of a matlab plot, and I'm satisfied with the rest automatic x-ticks that matlab produce. Therefore I do not want to change them, only to add this special xtick and xticklabel in the midst of them. What is the easiest way to do it in a matlab m-function?

Upvotes: 1

Views: 1899

Answers (2)

Jens Boldsen
Jens Boldsen

Reputation: 1275

Try something like this:

plot(1:100);
ticks = get(gca,'XTick');
ticklabels = cellstr(get(gca,'XTickLabel'));
ticks(end+1) = pi;
ticklabels{end+1} = 'Pi';
[ticks,idx] = sort(ticks);
ticklabels = ticklabels(idx);
set(gca,'Xtick',ticks,'XTickLabel',ticklabels);

Upvotes: 2

michael_0815
michael_0815

Reputation: 146

plot(1:5,1:5)
marks = get(gca,'XTick');
marks = sort([marks,pi]);
set(gca,'XTick',marks);

There are some issues with scaleability, calling xlim, etc. but maybe it is good enough for your problem.

Upvotes: 0

Related Questions