<P>How do I format tick labels? --From mathworks</P>
<P>Problem Description <BR>How do I format tick labels? </P>
<P>I have an axis with tick labels and I want them all to have 3.1 format. For example, I do a plot (1:5) and I change my tick labels using the command </P>
<P>set(gca, 'XTick', [1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0]) </P>
<P>However, they appear as: </P>
<P>[1 1.5 2 2.5 3 3.5 4 4.5 5] </P>
<P>How do I get them to appear as I typed them in the above command? </P>
<P><BR>Solution: <BR>Unfortunately, this is not a built-in feature of MATLAB, but it has been suggested to our development staff for a future release of MATLAB. </P>
<P>However, there are several workarounds available. </P>
<P>There is a function on our ftp site, called fortick.m that will accomplish this. It can be found at </P>
<P><a href="ftp://ftp.mathworks.com/pub/tech-support/solutions/s10804/" target="_blank" >ftp://ftp.mathworks.com/pub/tech-support/solutions/s10804/</A> <BR>PLEASE NOTE: This function is not officially supported by the MathWorks. It uses cell arrays, so this will only work on MATLAB 5.0 and later versions. </P>
<P>Additionally, there is another function, extendticklabel.m, available on the MATLAB Central File Exchange that performs similar tick label formatting. You can find the function at the following URL: </P>
<P><BR><a href="http://www.mathworks.com/matlabcentral/fileexchange/index.jsp" target="_blank" >http://www.mathworks.com/matlabcentral/fileexchange/index.jsp</A> </P>
<P>Perform a search using "ticklabel" as the description. This function is also not supported by the MathWorks. </P>
<P>Finally, you can use the SPRINTF function to format your tick labels using the format of your choice then set the XTickLabel or YTickLabel property of the axis to use those strings as tick labels. An example which demonstrates this is: </P>
<P>x=[1 1.53 4]; <BR>y=[1 2 3]; <BR>plot(x,y) <BR>set(gca,'XTick',x) <BR>set(gca,'XTickLabel',sprintf('%3.4f|',x)) <BR>set(gca,'YTick',y) <BR>set(gca,'YTickLabel',sprintf('%+1.2f|',y)) </P> |