Controlling format (number of decimals) of specific ticks

조회 수: 3 (최근 30일)
bloodtalon
bloodtalon 2017년 6월 21일
편집: dpb 2017년 6월 22일
I have a y axis running from 0 to 1 in the form of 0, 0.1, 0.2...1. I want to add an extra label, rounded to 3 decimals. By default, it's doing 4 decimals. The usual one liner to control decimals,
set(gca,'yticklabel',num2str(get(gca,'ytick')','%.3f'))
is doing it for all the points and making it ugly such as 0.100, 0.200...1.000. I just want the specific tick to have 3 decimals, not all of them. Is this possible?

채택된 답변

dpb
dpb 2017년 6월 21일
편집: dpb 2017년 6월 22일
figure
hAx=axes;
xtklab=get(hAx,'xticklabel');
xtklab{1}='0.000';
set(hAx,'xticklabel',xtklab)
results in
where just made arbitrary change. Obviously you can retrieve the actual tick values and convert them if don't know the actual value.
If you want to add another tick besides the default, you'll have to create those first then create the correct number of labels to match formatting them on a per each basis with the proper format string, then update.
Again, if it's known which one, doing the modification of the ticks themselves and letting the automagic labels be written first makes sense; after that just follow the above template to adjust just the one in the array wherever it is positioned.
The new axis ruler property has a 'Format' property, but it's global for the axis, not on a per tick basis.
ADDENDUM
So, for the given case...
NewTickVal=0.355; % the desired new tick value
hAx=axes;
ytk=sort([NewTickVal hAx.YTick]); % put it in the right place
hAx.YTick=ytk; % and update the tick positions
ytklab=hAx.YTickLabel; % get the new labels array
iy=find(ytk==NewTickVal); % find the one we want
ytklab{iy}=num2str(ytk(iy),'%.3f'); % and write in desired fmt
hAx.YTickLabel=ytklab; % reset labels array
The key thing to remember with tick labels vis a vis ticks is that they are NOT connected in any way whatsoever except by positional order. The label array is simply associated 1:1 in sequential order to the tick array position.
This also illustrates the use where it's convenient of the dot nomenclature for retrieving/setting properties. NB: with it, capitalization is significant where it is not with get set
  댓글 수: 8
bloodtalon
bloodtalon 2017년 6월 22일
OK so that mess was due to the axes for some reason. This code is working for the most part, except replacing my 1st value since I have {1}.
extick = round(y(1),3,'significant');
a=gca;
ytklab=get(a,'YTickLabel');
ytklab{1}=num2str(extick);
set(a,'YTickLabel',ytklab)
dpb
dpb 2017년 6월 22일
편집: dpb 2017년 6월 22일
None of the above except the one I had done before ever actually added the additional tick you're trying to label...the ticks must exist before you can put labels on them (seems obvious said that way?).

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by