필터 지우기
필터 지우기

changing the X tick label

조회 수: 16 (최근 30일)
alex
alex 2011년 11월 30일
hi everybody, I am looking for a way to change the X tick labels not by hand , because it's a lot of ticks to change' i want to do it by loop , I have to vectors a=[ 1 2 3 4 5] b = [ 10 9 8 7 6] and my X tick label now is 1 2 3 4 5, but i want it to be : 1-10 2-9 3-8 4-7 5-6, I guess it involves somehow num2str function but I am not sure how or if..

채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 30일
set(gca, 'XTickLabel', a-b)
Or if you prefer,
set(gca, 'XTickLabel', str2num(a(:)-b(:)) )
It is important for this purpose that the expression passed to str2num be a column vector rather than a row vector.

추가 답변 (2개)

Matt Tearle
Matt Tearle 2011년 11월 30일
If a and b are numeric, then
lbls = strcat(strtrim(cellstr(num2str(a(:)))),'-',strtrim(cellstr(num2str(b(:)))))
set(gca,'XTickLabel',lbls)
Ugly, but it gets rid of any excess spaces.

Kelly Kearney
Kelly Kearney 2011년 11월 30일
Perhaps a little less ugly that Matt's suggestion (though not by much):
lbl = arrayfun(@(x,y) sprintf('%d-%d',x,y), a, b, 'uni', 0);
set(gca, 'xticklabel', lbl);
  댓글 수: 1
Matt Tearle
Matt Tearle 2011년 12월 1일
Ooh arrayfun. Cute. This was my sprintf solution:
lbls = regexp(sprintf('%d-%d;',[a(:),b(:)]'),';','split');
set(gca,'XTIckLabel',lbls(1:end-1))

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

카테고리

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