The functions listed in this table provide a number of ways to convert numeric data to character arrays.
Function | Description | Example |
---|---|---|
Convert a positive integer to an equivalent character. (Truncates any fractional parts.) |
| |
Convert an array of double-precision values to a string array. |
| |
Convert a positive or negative integer to a character type. (Rounds any fractional parts.) |
| |
Convert a numeric type to a character type of the specified precision and format. |
| |
Convert a numeric type to a character type of the specified precision, returning a character vector MATLAB® can evaluate. |
| |
Convert a positive integer to a character type of hexadecimal base. |
| |
Convert a positive integer to a character type of binary base. |
| |
Convert a positive integer to a character type of any base from 2 through 36. |
|
The char
function converts
integers to Unicode® character codes and returns a character array
composed of the equivalent characters:
x = [77 65 84 76 65 66]; char(x) ans = 'MATLAB'
The int2str
, num2str
, and mat2str
functions
represent numeric values as text where each character represents a
separate digit of the input value. The int2str
and num2str
functions
are often useful for labeling plots. For example, the following lines
use num2str
to prepare automated labels for the x-axis
of a plot:
function plotlabel(x, y) plot(x, y) chr1 = num2str(min(x)); chr2 = num2str(max(x)); out = ['Value of f from ' chr1 ' to ' chr2]; xlabel(out);
Another class of conversion functions changes numeric values
into character arrays representing a decimal value in another base,
such as binary or hexadecimal
representation. This includes dec2hex
, dec2bin
, and dec2base
.