Code covered by the BSD License  

Highlights from
NumberFormatter

3.0

3.0 | 2 ratings Rate this file 10 Downloads (last 30 days) File Size: 1.53 KB File ID: #8361
image thumbnail

NumberFormatter

by Stephen Lienhard

 

24 Aug 2005 (Updated 25 Aug 2005)

Flexible number formatting using java.

| Watch this File

File Information
Description

Here is a simple example how to use java.text to format numbers using similar syntax to Excel's. # for a number or space, 0 for a number or zero, etc...

I found this useful for plots. I wanted the ability to have tic labels formatted with commas. Rather then writing my own, I use the ability to call java from matlab.

Example:

 NumberFormatter(1e4*rand(5,1),'0,000.000')
ans =
    '7,452.920'
    '6,511.113'
    '4,545.918'
    '5,231.557'
    '5,120.765'

To utilize with plotting:

plot(1:5,2:6);
XTicksVec=str2num(get(gca,'XTickLabel'));
XTicksCell=NumberFormatter(XTicksVec,'0.0');
set(gca,'XTickLabel',XTicksCell);

This works as is for my needs, but I welcome any suggestions for improvement.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
06 Oct 2006 Matlab User

does not solve the problem...

09 Oct 2006 A User

Works just fine for me.

04 Apr 2007 Stephen Lienhard

Here are some examples of the syntax:
http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
commas Stephen Lienhard 22 Oct 2008 07:56:36
formatting Stephen Lienhard 22 Oct 2008 07:56:36
plot Stephen Lienhard 22 Oct 2008 07:56:36
miscellaneous Stephen Lienhard 22 Oct 2008 07:56:36
utilities Stephen Lienhard 22 Oct 2008 07:56:36

Contact us at files@mathworks.com