File Exchange

image thumbnail

num2str with metric prefix

version 1.0.0.0 (1.37 KB) by Mark T
Converts numbers to strings in engineering notation

0 Downloads

Updated 12 Aug 2008

View License

Converts numbers to strings in engineering notation. Produces string (array) from numbers with mantissa and metric prefix (every third power of 10 between 1e-24 to 1e24).

Comments and Ratings (7)

Disclaimer: In the end I could not find any FEX submission that gave the correct outputs for the wide range of test-cases that I tried, so I ended up writing my own function. You can find it here:
http://www.mathworks.com/matlabcentral/fileexchange/33174

This submission uses an excellent concept to achieve its goal, and the output strings are pretty close to what is expected: for example the strings always correctly include the space character (even if there is no prefix), and it handles out-of-prefix-range values gracefully. The precision control is handy, but for sigfigs<3 it can return very awkward strings (eg: 1e4 -> '1e+001 k', should simply be '10 k', for 1 sigfig). The main drawback of this submission is the use of "feval", which makes it slow and hard to debug. The addition of an H1 line, some input checking and replacing that superfluous usage of <varargin> with a normal input variable would help tidy the code up, but basically it does what it says on the box.

David

Very useful. However, there is an error when the input vector contains both a value that needs a prefix, and one that does not. (i.e. [10 0.001]).

Modification to output cell arrays instead of string arrays will fix the error.

Alternatively, changing strvcat to char will also fix the error, but you'll be left with an extra space on those values without a prefix.

Anthony

Adriano

hm doesnt seems handle the certain tick that is zero

Very nice

Updates

1.0.0.0

Incorrect use of cat, instead of strvcat was causing error for some input vectors...oops!

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux