Insert xticks in bold in existing xticks

조회 수: 13 (최근 30일)
mael thevenot
mael thevenot 2018년 9월 5일
댓글: mael thevenot 2018년 9월 10일
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?

답변 (1개)

dpb
dpb 2018년 9월 5일
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
dpb
dpb 2018년 9월 6일
편집: dpb 2018년 9월 6일
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 2018년 9월 10일
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 :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by