Rotates tick labels TH=ROTATETICKLABEL(H,ROT) is the calling form where H is a handle to the axis that contains the XTickLabels that are to be rotated. ROT is an optional parameter that specifies the angle of rotation. The default angle is 90. TH is a handle to the text objects created. For long strings such as those produced by datetick, you may have to adjust the position of the axes so the labels don't get cut off. Of course, GCA can be substituted for H if desired. TH=ROTATETICKLABEL([],[],'demo') shows a demo figure. Known deficiencies: if tick labels are raised to a power, the power will be lost after rotation. See also datetick.
0001 function th=rotateticklabel(h,rot,demo) 0002 % Rotates tick labels 0003 % 0004 % TH=ROTATETICKLABEL(H,ROT) is the calling form where H is a handle to 0005 % the axis that contains the XTickLabels that are to be rotated. ROT is 0006 % an optional parameter that specifies the angle of rotation. The default 0007 % angle is 90. TH is a handle to the text objects created. For long 0008 % strings such as those produced by datetick, you may have to adjust the 0009 % position of the axes so the labels don't get cut off. 0010 % 0011 % Of course, GCA can be substituted for H if desired. 0012 % 0013 % TH=ROTATETICKLABEL([],[],'demo') shows a demo figure. 0014 % 0015 % Known deficiencies: if tick labels are raised to a power, the power 0016 % will be lost after rotation. 0017 % 0018 % See also datetick. 0019 0020 % Written Oct 14, 2005 by Andy Bliss 0021 % Copyright 2005 by Andy Bliss 0022 % 0023 % S.T. - minor modification to rotate yticklabels instead of xticklabels 0024 0025 %DEMO: 0026 if nargin==3 0027 x=[now-.7 now-.3 now]; 0028 y=[20 35 15]; 0029 figure 0030 plot(x,y,'.-') 0031 datetick('x',0,'keepticks') 0032 h=gca; 0033 set(h,'position',[0.13 0.35 0.775 0.55]) 0034 rot=90; 0035 end 0036 0037 %set the default rotation if user doesn't specify 0038 if nargin==1 0039 rot=90; 0040 end 0041 %make sure the rotation is in the range 0:360 (brute force method) 0042 while rot>360 0043 rot=rot-360; 0044 end 0045 while rot<0 0046 rot=rot+360; 0047 end 0048 %get current tick labels 0049 a=get(h,'YTickLabel'); 0050 0051 %get label axis location 0052 labelLoc = get(h,'yaxislocation'); 0053 %erase current tick labels from figure 0054 set(h,'YTickLabel',[]); 0055 %get tick label positions 0056 b=get(h,'XLim'); 0057 c=get(h,'YTick'); 0058 0059 if(strcmp(labelLoc,'right')) 0060 b = [b(2) b(1)]; 0061 offset = 0.1; 0062 else 0063 offset = 0.032; 0064 end 0065 0066 0067 %make new tick labels 0068 if rot<180 0069 th=text(repmat(b(1)-offset*(b(2)-b(1)),length(c),1),c,a,'HorizontalAlignment','left','verticalalignment','middle','rotation',rot); 0070 else 0071 th=text(repmat(b(1)-offset*(b(2)-b(1)),length(c),1),c,a, ... 0072 'HorizontalAlignment','right','verticalalignment','middle','rotation',rot); 0073 0074 end 0075 0076