Insert xticks in bold in existing xticks

13 views (last 30 days)
mael thevenot
mael thevenot on 5 Sep 2018
Commented: mael thevenot on 10 Sep 2018
Hi,
I would like to make some change to the xticks of my plot without doing it manually.
So far i'm changing xticks of certain values by a text with :
names = {'text1'; 'text2'; 'text3'};
plot(...
set(gca,'xtick',[position1, position2, position3],'xticklabel',names)
The downside is that now I only have those 3 texts displaying in the x axis. I would like to have the default ticks PLUS those texts at those positions (in bold if possible). And just erase default ticks that are at those 3 positions.
Example if I have on the x axis the labels:
1, 2, 3, 4, 5, 6, 7, 8
I would like to have :
1, 2, text1, 4, 5, text2, 7, text3
How can I do this?

Answers (1)

dpb
dpb on 5 Sep 2018
You've got to write the same number of tick labels as ticks and place the non-automatic text in the locations desired, explicitly. There's not sufficient syntax to write a portion of the 'XTickLabel' array alone.
hAx=axes; % make an axes to play with
xlim([1 8]) % set limits
xlbl=xticklabels; % retrieve default labels
ix=[3 6 8]; % define those to change
for i=1:length(ix) % make up the new ones for those locations...
xlbl(ix(i))={num2str(i,'\\bfText%d')};
end
xticklabels(xlbl) % and write them back.
  3 Comments
dpb
dpb on 6 Sep 2018
Edited: dpb on 6 Sep 2018
doc xticklabels
...
xl = xticklabels returns the x-axis tick labels for the current axes.
...
Introduced in R2016b
If your release is earlier, then you'll have to use
xlbl=hAx.XTickLabel;
or if even earlier than R2014, then
xlbl=get(hAx,'XTickLabel');
Somehow you've got to get the existing tick label array to operate on.
hAx=axes;
just created an axis to demonstrate with; use the handle to the axes object you're trying to add the labels. For the current axes, use
hAx=gca;
to get the handle into a variable instead of calling the function every time; gca can (and will) change which axes is returned if focus is changed, saving the handle to a variable ensures you're writing to the one intended.
mael thevenot
mael thevenot on 10 Sep 2018
Ok thanks, yeah I'm using R2015b, so the
xlbl=hAx.XTickLabel;
worked. Also now I've understood that I have to use
hAx=gca;
So right now my code is :
hAx=gca; % make an axes to play with
xlbl=hAx.XTickLabel;; % retrieve default labels
ix=[value1 value2 value3]; % define those to change
% for i=1:length(ix) % make up the new ones for those locations...
% xlbl(ix(i))={num2str(i,'\\bfText%d')};
% end
The only thing I did not success to do is to write those values back in my axis, since I can't use
xticklabels(xlbl)
How do I do that? I've tried some things like
hAx.XTickLabel(xlbl)
but I always have an error, like for this one: Function 'subsindex' is not defined for values of class 'cell'.
Do you know how to re-write this command in the 2015 release? I did not find the answer in the documentation. Many thanks for your help so far :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!