Convert Numeric Values to Text

This example shows how to convert numeric values to text and append them to larger pieces of text. For example, you might want to add a label or title to a plot, where the label includes a number that describes a characteristic of the plot.

You can convert numeric values to strings or character vectors. You can convert them to decimal, hexadecimal, or binary text representations. The same techniques also convert datetime or duration values to text.

Numeric Arrays

To convert a number to a string that represents it, use the string function.

A = 3.1416
A = 3.1416
str = string(A)
str = 

The string function converts a numeric array to a string array having the same size.

A = [256 3.1416 8.9e-3]
A = 1×3

  256.0000    3.1416    0.0089

str = string(A)
str = 1x3 string
    "256"    "3.1416"    "0.0089"

While the char function also converts numbers to characters, it does not perform the same conversion as string. Rather, it converts numbers to individual characters (for example, converting 65 to the letter A). It is not recommended for converting floating-point numbers to strings that represent them.

You can specify the format of the output text using the compose, sprintf, or num2str functions. These functions accept format specifiers so that you can specify precision, field width, and exponential notation.

str = compose("%9.7f",pi)
str = 

If the input is a numeric array, then compose returns a string array. Return a string array that represents numbers using exponential notation.

A = [256 3.1416 8.9e-3];
str = compose("%5.2e",A)
str = 1x3 string
    "2.56e+02"    "3.14e+00"    "8.90e-03"

The compose, sprintf, and num2str functions return different types of output.

  • compose returns a string array that has the same size as the input numeric array.

  • sprintf returns a character vector or string scalar, even when the input is a numeric array.

  • num2str always returns a character vector.

The best practice is to use either the string or compose function to convert values.

  • Use string to convert numeric values.

  • Use compose to also control the format of output text.

  • Avoid the char function. It treats numbers as integers representing Unicode values and converts them to the corresponding characters.

Append Converted Numeric Values to Text

A typical use for these functions is to convert numbers to text so that you can append them to other pieces of text, such as file names or labels on a plot.

For example, plot a sine wave and add a title to the plot. Calculate the frequency of the wave and add a string representing that value in the title of the plot.

X = linspace(0,2*pi);
Y = sin(X);
freq = string(1/(2*pi));
titleStr = append("Sine Wave, Frequency = ",freq," Hz")
titleStr = 
"Sine Wave, Frequency = 0.15915 Hz"

As an alternative, you can use the plus operator to add text to the end of a string. However, plus treats character vectors like numeric arrays.

titleStr = "Sine Wave, Frequency = " + freq + " Hz"
titleStr = 
"Sine Wave, Frequency = 0.15915 Hz"

The append function appends text to string arrays, character vectors, and cell arrays of character vectors. For this reason, the best practice is to use the append function.

Hexadecimal and Binary Values

You can represent hexadecimal and binary values in your code either using text or using literals. The recommended way to represent them is to write them as literals. You can write hexadecimal and binary literals using the 0x and 0b prefixes respectively. However, it can sometimes be useful to represent such values as text, using the dec2hex or dec2bin functions.

For example, set a bit in a binary value. If you specify the binary value using a literal, then it is stored as an integer. After setting one of the bits, display the new binary value as text using the dec2bin function.

register = 0b10010110
register = uint8
register = bitset(register,5,0)
register = uint8
binStr = dec2bin(register)
binStr = 

Dates and Times

MATLAB provides the datetime and duration data types to store dates and times, and to treat them as numeric values. To convert such values to text representing dates and times, use the string and char functions.

Represent the current date and time as a string.

d = datetime('now')
d = datetime
   29-Feb-2020 00:09:48

str = string(d)
str = 
"29-Feb-2020 00:09:48"

Represent an array of duration values.

D = seconds(1:5)
D = 1x5 duration
   1 sec   2 sec   3 sec   4 sec   5 sec

str = string(D)
str = 1x5 string
    "1 sec"    "2 sec"    "3 sec"    "4 sec"    "5 sec"

See Also

| | | | | | | | | | | |

Related Topics