Numeric to Ordinal-String
version 2.1.3 (5.2 KB) by
Stephen23
Convert numeric values to a string array of integers with ordinal suffixes. Fully vectorized!
Convert a numeric array into a string array of the rounded values with ordinal suffixes, e.g. 1 -> "1st".
Similar to MATLAB's IPTNUM2ORDINAL (image toolbox) and NUM2ORDINAL (mapping toolbox) and other files available on FEX, except that NUM2ORD:
- accepts a numeric array of any size,
- accepts a numeric array of any class (float, int or uint),
- returns a string array (not a character array!),
- is fully vectorized (no loops or ARRAYFUN calls are used or required),
- provides the correct ordinal suffixes for numeric values ending in 11, 12, or 13.
The optional second input selects between returning the integer and ordinal suffix (default), or just the ordinal suffix by itself.
Character Output
For MATLAB versions prior to R2017a please download version 1.5.2 (with character array output).
Examples
>> num2ord(1)
ans = "1st"
>> num2ord(1:6)
ans = ["1st","2nd","3rd","4th","5th","6th"]
>> num2ord([1;11;111;1111])
ans = ["1st";"11th";"111th";"1111th"]
>> num2ord(100:113,true)
ans = ["th","st","nd","rd","th","th","th","th","th","th","th","th","th","th"]
>> num2ord(intmax('int64')-4)
ans = "9223372036854775803rd"
>> num2ord([-1,-0,0;-Inf,NaN,Inf])
ans = ["-1th","-0th","0th";"-Infth","NaNth","Infth"]
Cite As
Stephen23 (2022). Numeric to Ordinal-String (https://www.mathworks.com/matlabcentral/fileexchange/42833-numeric-to-ordinal-string), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2010b
Compatible with R2017a and later releases
Platform Compatibility
Windows macOS LinuxCategories
Tags
Acknowledgements
Inspired by: Number to Ordinal String Converter, ORDNUMSTR, Number to Scientific Prefix, Customizable Natural-Order Sort, Number to Words, Words to Number
Inspired: Words to Number, Numeric to Yllion, Number to Words, Interactive Regular Expression Tool
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.