matthiasm@8: function xticklabel_rotate90(XTick,varargin) matthiasm@8: %XTICKLABEL_ROTATE90 - Rotate numeric Xtick labels by 90 degrees matthiasm@8: % matthiasm@8: % Syntax: xticklabel_rotate90(XTick) matthiasm@8: % matthiasm@8: % Input: XTick - vector array of XTick positions & values (numeric) matthiasm@8: % matthiasm@8: % Output: none matthiasm@8: % matthiasm@8: % Example 1: Set the positions of the XTicks and rotate them matthiasm@8: % figure; plot([1960:2004],randn(45,1)); xlim([1960 2004]); matthiasm@8: % xticklabel_rotate90([1960:2:2004]); matthiasm@8: % %If you wish, you may set a few text "Property-value" pairs matthiasm@8: % xticklabel_rotate90([1960:2:2004],'Color','m','Fontweight','bold'); matthiasm@8: % matthiasm@8: % Example 2: %Rotate XTickLabels at their current position matthiasm@8: % XTick = get(gca,'XTick'); matthiasm@8: % xticklabel_rotate90(XTick); matthiasm@8: % matthiasm@8: % Other m-files required: none matthiasm@8: % Subfunctions: none matthiasm@8: % MAT-files required: none matthiasm@8: % matthiasm@8: % See also: TEXT, SET matthiasm@8: matthiasm@8: % Author: Denis Gilbert, Ph.D., physical oceanography matthiasm@8: % Maurice Lamontagne Institute, Dept. of Fisheries and Oceans Canada matthiasm@8: % email: gilbertd@dfo-mpo.gc.ca Web: http://www.qc.dfo-mpo.gc.ca/iml/ matthiasm@8: % February 1998; Last revision: 24-Mar-2003 matthiasm@8: matthiasm@8: if ~isnumeric(XTick) matthiasm@8: error('XTICKLABEL_ROTATE90 requires a numeric input argument'); matthiasm@8: end matthiasm@8: matthiasm@8: %Make sure XTick is a column vector matthiasm@8: XTick = XTick(:); matthiasm@8: matthiasm@8: %Set the Xtick locations and set XTicklabel to an empty string matthiasm@8: set(gca,'XTick',XTick,'XTickLabel','') matthiasm@8: matthiasm@8: % Define the xtickLabels matthiasm@8: xTickLabels = num2str(XTick); matthiasm@8: matthiasm@8: % Determine the location of the labels based on the position matthiasm@8: % of the xlabel matthiasm@8: hxLabel = get(gca,'XLabel'); % Handle to xlabel matthiasm@8: xLabelString = get(hxLabel,'String'); matthiasm@8: matthiasm@8: if ~isempty(xLabelString) matthiasm@8: warning('You may need to manually reset the XLABEL vertical position') matthiasm@8: end matthiasm@8: matthiasm@8: set(hxLabel,'Units','data'); matthiasm@8: xLabelPosition = get(hxLabel,'Position'); matthiasm@8: y = xLabelPosition(2); matthiasm@8: matthiasm@8: %CODE below was modified following suggestions from Urs Schwarz matthiasm@8: y=repmat(y,size(XTick,1),1); matthiasm@8: % retrieve current axis' fontsize matthiasm@8: fs = get(gca,'fontsize'); matthiasm@8: matthiasm@8: % Place the new xTickLabels by creating TEXT objects matthiasm@8: hText = text(XTick, y, xTickLabels,'fontsize',fs); matthiasm@8: matthiasm@8: % Rotate the text objects by 90 degrees matthiasm@8: set(hText,'Rotation',90,'HorizontalAlignment','right',varargin{:}) matthiasm@8: matthiasm@8: %------------- END OF CODE --------------