wolffd@0: function set_xtick_label(tick_labels, angle, axis_label) wolffd@0: % SET_XTICK_LABEL Print the xtick labels at an angle instead of horizontally wolffd@0: % set_xtick_label(tick_labels, angle, axis_label) wolffd@0: % wolffd@0: % angle default = 90 wolffd@0: % axis_label default = '' wolffd@0: % wolffd@0: % This is derived from Solution Number: 5375 on mathworks.com wolffd@0: % See set_xtick_label_demo for an example wolffd@0: wolffd@0: if nargin < 2, angle = 90; end wolffd@0: if nargin < 3, axis_label = []; end wolffd@0: wolffd@0: % Reduce the size of the axis so that all the labels fit in the figure. wolffd@0: pos = get(gca,'Position'); wolffd@0: %set(gca,'Position',[pos(1), .2, pos(3) .65]) wolffd@0: %set(gca,'Position',[pos(1), 0, pos(3) .45]) wolffd@0: %set(gca,'Position',[pos(1), 0.1, pos(3) 0.5]) wolffd@0: wolffd@0: ax = axis; % Current axis limits wolffd@0: axis(axis); % Fix the axis limits wolffd@0: Yl = ax(3:4); % Y-axis limits wolffd@0: wolffd@0: %set(gca, 'xtick', 1:length(tick_labels)); wolffd@0: set(gca, 'xtick', 0.7:1:length(tick_labels)); wolffd@0: Xt = get(gca, 'xtick'); wolffd@0: wolffd@0: % Place the text labels wolffd@0: t = text(Xt,Yl(1)*ones(1,length(Xt)),tick_labels); wolffd@0: set(t,'HorizontalAlignment','right','VerticalAlignment','top', 'Rotation', angle); wolffd@0: wolffd@0: % Remove the default labels wolffd@0: set(gca,'XTickLabel','') wolffd@0: wolffd@0: % Get the Extent of each text object. This wolffd@0: % loop is unavoidable. wolffd@0: for i = 1:length(t) wolffd@0: ext(i,:) = get(t(i),'Extent'); wolffd@0: end wolffd@0: wolffd@0: % Determine the lowest point. The X-label will be wolffd@0: % placed so that the top is aligned with this point. wolffd@0: LowYPoint = min(ext(:,2)); wolffd@0: wolffd@0: % Place the axis label at this point wolffd@0: if ~isempty(axis_label) wolffd@0: Xl = get(gca, 'Xlim'); wolffd@0: XMidPoint = Xl(1)+abs(diff(Xl))/2; wolffd@0: tl = text(XMidPoint,LowYPoint, axis_label, 'VerticalAlignment','top', ... wolffd@0: 'HorizontalAlignment','center'); wolffd@0: end