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