File Exchange

image thumbnail

Numeric to Ordinal-String

version (2.22 KB) by Stephen Cobeldick
Convert numeric values to a character array of integers with ordinal suffixes. Fully vectorized!


Updated 26 Jun 2016

View License

Convert a numeric scalar/vector/matrix/array into a character array giving the integers and ordinal suffixes of the rounded input element values, for example the value 1 is returned as '1st'.

Similar to MATLAB's IPTNUM2ORDINAL and other files available on FEX, except that this function accepts numeric arrays (not just scalars) of any class (float, int or uint) and is fully vectorized (no loops or ARRAYFUN calls are used or required). The function rounds fractional numeric values to the nearest integer, and provides the correct suffixes for numeric values ending in 11, 12 or 13. The optional second input selects between returning the integer and suffix (default), or just the suffix (fast!).

### Examples ###

ans = '1st'

ans = ['1st';'2nd';'3rd';'4th';'5th';'6th']

ans = [' 1st';' 11th';' 111th';'1111th']

ans = ['th';'st';'nd';'rd';'th';'th';'th';'th';'th';'th';'th';'th';'th';'th']

ans = '9223372036854775803rd'

ans = ['-0th';' 0th']

vals = [-1,-0,0;-Inf,NaN,Inf];
ans = {'-1th','-0th','0th';'-Infth','NaNth','Infth'}

### Note ###

Compared to MATLAB inbuilt functions and several other M-files that are available on MATLAB File Exchange, this file:
- Correctly converts all numeric values ending in 11, 12, or 13.
- Accepts a scalar/array numeric.
- Is fully vectorized.
- Optionally returns only the ordinal suffix (fast!).
- Returns the complete digits for uint and int type numerics.
- Distinguishes between positive and negative zero.

Comments and Ratings (1)

Pi Ting

Great job, thanks!


* Improve documentation.

Added Acknowledgements

- Fix minor bug with int* and uint*.

- Improve function description, add acknowledgements.

- Fix minor bugs with -0, int* & uint* handling.

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

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor