Convert numbers to character array
Convert the floating-point values returned by pi
and eps
to character vectors.
s = num2str(pi)
s = '3.1416'
s = num2str(eps)
s = '2.2204e-16'
Specify the maximum number of significant digits for floating-point values.
A = gallery('normaldata',[2,2],0);
s = num2str(A,3)
s = 2x16 char array
'-0.433 0.125'
' -1.67 0.288'
Specify the width, precision, and other formatting for an array of floating-point values.
A = gallery('uniformdata',[2,3],0) * 9999; s = num2str(A,'%10.5e\n')
s = 2x35 char array
'9.50034e+03...'
'2.31115e+03...'
The format '%10.5e'
prints each value in exponential format with five decimal places, and '\n'
prints a new line character.
A
— Input arrayInput array, specified as a numeric array.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
precision
— Maximum number of significant digitsMaximum number of significant digits in the output string, specified as a positive integer.
If you specify precision
to exceed the precision
of the input floating-point data type, the results might not match
the input values to the precision you specified. The result depends
on your computer hardware and operating system.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
formatSpec
— Format of output fieldsFormat of the output fields, specified using formatting operators. formatSpec
also
can include ordinary text and special characters.
formatSpec
can be a character vector in single
quotes, or, starting in R2016b, a string scalar.
Formatting Operator
A formatting operator starts with a percent sign, %
,
and ends with a conversion character. The conversion character is
required. Optionally, you can specify identifier, flags, field width,
precision, and subtype operators between %
and
the conversion character. (Spaces are invalid between operators and
are shown here only for readability).
Conversion Character
This table shows conversion characters to format numeric and character data as text.
Value Type | Conversion | Details |
---|---|---|
Integer, signed |
| Base 10 |
Integer, unsigned |
| Base 10 |
| Base 8 (octal) | |
| Base 16 (hexadecimal), lowercase letters | |
| Same as | |
Floating-point number |
| Fixed-point notation (Use a precision operator to specify the number of digits after the decimal point.) |
| Exponential notation, such as | |
| Same as | |
| The more compact of | |
| The more compact of | |
Characters or strings |
| Single character |
| Character vector or string array. The type of the output
text is the same as the type of |
Optional Operators
The optional identifier, flags, field width, precision, and subtype operators further define the format of the output text.
Identifier
Order for processing the function input arguments. Use the syntax
,
where n
$n
represents the positions of the
other input arguments in the function call.
Example: ('%3$s %2$s
%1$s %2$s','A','B','C')
prints input arguments 'A'
, 'B'
, 'C'
as
follows: C B A B
.
Note: If an input argument is an array, you cannot use identifiers to specify particular array elements from that input argument.
Flags
| Left-justify. |
| Always print a sign character (+ or –) for any
numeric value. |
| Insert a space before the value. |
| Pad to field width with zeros before the value. |
| Modify selected numeric conversions:
Example: |
Field Width
Minimum number of characters to print. The field width operator can be a number, or an
asterisk (*
) to refer to an input argument.
When you specify *
as the field width operator, the other
input arguments must provide both a width and a value to be printed. Widths and
values can be pairs of arguments or pairs within a numeric array. With
*
as the field width operator, you can print different
values with different widths.
Example: The input arguments
('%12d',intmax)
are equivalent to
('%*d',12,intmax)
.
Example: The input arguments
('%*d',[2 10 5 100])
return '10 100'
,
with two spaces allocated for 10
and five spaces for
100
. As an alternative, you also can specify the field
widths and values as multiple arguments, as in
('%*d',2,10,5,100)
.
The function pads to field width with spaces before the value unless otherwise specified by flags.
Precision
For | Number of digits to the right of the decimal point |
For | Number of significant digits |
The precision operator can be a number, or an asterisk (*
)
to refer to an argument.
When you specify *
as the field precision operator, the
other input arguments must provide both a precision and a value to be printed.
Precisions and values can be pairs of arguments, or pairs within a numeric
array. With *
as the precision operator, you can print
different values to different precisions.
When you specify *.*
as field width and precision
operators, you must specify field widths, precisions, and values as
triplets.
Example: The input arguments ('%.4f',pi)
are equivalent to ('%.*f',4,pi)
.
Example: The input arguments
('%6.4f',pi)
are equivalent to
('%.*f',6,4,pi)
.
Example: The input arguments
('%*.*f',6,4,pi,9,6,exp(1))
return '3.1416
2.718282'
, with 9
and 6
as
the field width and precision for the output of
exp(1)
.
If you specify a precision operator for floating-point values that exceeds the precision of the input numeric data type, the results might not match the input values to the precision you specified. The result depends on your computer hardware and operating system.
Subtypes
You can use a subtype operator to print a floating-point value as its octal, decimal, or hexadecimal value. The subtype operator immediately precedes the conversion character. This table shows the conversions that can use subtypes.
Input Value Type | Subtype and Conversion Character | Output Value Type |
---|---|---|
Floating-point number |
| Double-precision hexadecimal, octal, or decimal value |
| Single-precision hexadecimal, octal, or decimal value |
Text Before or After Formatting Operators
formatSpec
can also include additional text
before a percent sign, %
, or after a conversion
character. The text can be:
Ordinary text to print.
Special characters that you cannot enter as ordinary
text. This table shows how to represent special characters in formatSpec
.
Special Character | Representation |
---|---|
Single quotation mark |
|
Percent character |
|
Backslash |
|
Alarm |
|
Backspace |
|
Form feed |
|
New line |
|
Carriage return |
|
Horizontal tab |
|
Vertical tab |
|
Character whose Unicode® numeric value can be represented
by the hexadecimal number, |
Example: |
Character whose Unicode numeric value can be represented
by the octal number, |
Example: |
Notable Behavior of Conversions with Formatting Operators
Numeric conversions print only the real component of complex numbers.
If you specify a conversion that does not fit the
data, such as a text conversion for a numeric value, MATLAB® overrides
the specified conversion, and uses %e
.
Example: '%s'
converts pi
to 3.141593e+00
.
If you apply a text conversion (either %c
or %s
)
to integer values, MATLAB converts values that correspond to
valid character codes to characters.
Example: '%s'
converts [65
66 67]
to ABC
.
Data Types: char
| string
s
— Text representation of input arrayText representation of the input array, returned as a character array.
num2str
does not accept positional identifiers
in the formatSpec
input argument. For example, num2str([14
15],'%2$X %1$o)
returns an error.
Positional identifiers specify the order in which the formatting
operator processes input arguments of the function, not the elements
of an input array. When you call num2str
, there
is only one input argument that has numbers to convert.
num2str
trims any leading spaces from a
character array, even when formatSpec
includes
a space character flag. For example, num2str(42.67,'% 10.2f')
returns
a 1-by-5 character array '42.67'
.
Usage notes and limitations:
The input arguments must be constants.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
You have a modified version of this example. Do you want to open this example with your edits?