s = num2str(A) convertsa numeric array inkhổng lồ a character array that represents the numbers.The output format depends on the magnitudes of the original values. num2str isuseful for labeling và titling plots with numeric values.

Bạn đang xem: Numeric là gì

s = num2str(A,precision) returnsa character array that represents the numbers with the maximum numberof significant digits specified by precision.

Input array, specified as a numeric array.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logicalComplex Number Support: Yes


If you specify precision lớn exceed the precisionof the input đầu vào floating-point data type, the results might not matchthe đầu vào values to the precision you specified. The result dependson your computer hardware và operating system.

Format of the output fields, specified using formatting operators. formatSpec also can include ordinary text & special characters.

If formatSpec includes literal text representing escape characters, such as , then num2str translates the escape 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 % và the conversion character. (Spaces are invalid between operators & are shown here only for readability).


Conversion Character

This table shows conversion characters khổng lồ format numeric and character data as text.

Value TypeConversionDetails

Integer, signed

%d or %i

Base 10

Integer, unsigned


Base 10


Base 8 (octal)


Base 16 (hexadecimal), lowercase letters a–f


Same as %x, uppercase letters A–F

Floating-point number


Fixed-point notation (Use a precision operator lớn specify the number of digits after the decimal point.)


Exponential notation, such as 3.141593e+00 (Use a precision operator khổng lồ specify the number of digits after the decimal point.)


Same as %e, but uppercase, such as 3.141593E+00 (Use a precision operator to specify the number of digits after the decimal point.)


The more compact of %e or %f, with no trailing zeros (Use a precision operator khổng lồ specify the number of significant digits.)


The more compact of %E or %f, with no trailing zeros (Use a precision operator lớn specify the number of significant digits.)

Characters or strings


Single character


Character vector or string array. The type of the output text is the same as the type of formatSpec.

Optional Operators

The optional identifier, flags, field width, precision, & subtype operators further define the format of the output text.


Note: Unlượt thích the sprintf function, num2str does not support identifiers.



Left-justify.Example: %-5.2fExample: %-10s


Always print a sign character (+ or –) for any numeric value.Example: %+5.2fRight-justify text.Example: %+10s

" "

Insert a space before the value.Example: % 5.2f


Pad to lớn field width with zeros before the value.Example: %05.2f


Modify selected numeric conversions:

For %o, %x, or %X, print 0, 0x, or 0X prefix.

For %f, %e, or %E, print decimal point even when precision is 0.

For %g or %G, vày not remove sầu trailing zeros or decimal point.

Example: %#5.0f

Field Width

Minimum number of characters to lớn print.

Example: "%5d" prints intmax as 2147483647 because the value returned by intmax exceeds the minimum number of characters lớn print.

If the number of characters lớn print is less than the field width, then the compose function pads khổng lồ the field width with spaces before the value unless otherwise specified by flags.

However, the num2str function does not pad khổng lồ the field width with spaces.


Number of digits to print.

Xem thêm: Cùng Tìm Hiểu Về Graphql Là Gì? Bắt Đầu Với Graphql Là Gì? Bắt Đầu Với Graphql

For %f, %e, or %E

Number of digits khổng lồ the right of the decimal pointExample: "%.4f" prints pi as "3.1416"

For %g or %G

Number of significant digitsExample: "%.4g" prints pi as "3.142"

Example: "%6.4f" prints pi as "3.1416".


If you specify a precision operator for floating-point values that exceeds the precision of the input đầu vào numeric data type, the results might not match the input values khổng lồ the precision you specified. The result depends on your computer hardware và operating system.


You can use a subtype operator to lớn 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 đầu ra Value Type

Floating-point number

%bx or %bX%bo%bu

Double-precision hexadecimal, octal, or decimal valueExample: %bx prints pi as 400921fb54442d18

%tx or %tX%to%tu

Single-precision hexadecimal, octal, or decimal valueExample: %tx prints pi as 40490fdb

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 khổng lồ print.

Special characters that you cannot enter as ordinary text. This table shows how to represent special characters in formatSpec.

Special Character


Single quotation mark


Percent character







Form feed


New line

Carriage return

Horizontal tab

Vertical tab


Character whose Unicode® numeric value can be represented by the hexadecimal number, N


Example: sprintf("x5A") returns "Z"

Character whose Unicode numeric value can be represented by the octal number, N


Example: sprintf("132") returns "Z"

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, và uses %e.

Example: "%s" converts pi khổng lồ 3.141593e+00.

If you apply a text conversion (either %c or %s) to integer values, MATLAB converts values that correspond khổng lồ valid character codes to lớn characters.

Example: "%s" converts <65 66 67> khổng lồ ABC.

Output đầu ra Arguments

collapse all

s — Text representation of đầu vào array character array

Text representation of the đầu vào array, returned as a characterarray.


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 đầu vào arguments of the function, not the elements of an đầu vào array. When you hotline num2str, there is only one đầu vào argument that has numbers to lớn convert.

If you specify an invalid formatting operator or special character, then num2str prints all text up khổng lồ the invalid operator or character & discards the rest.

Example: If formatSpec is "value = %z", then num2str prints "value =" because %z is not a formatting operator.

Example: If formatSpec is "character x99999 = %s", then num2str prints "character" because x99999 is not a valid special character.


num2str trims any leading spaces from acharacter array, even when formatSpec includesa space character flag. For example, num2str(42.67,"% 10.2f") returnsa 1-by-5 character array "42.67".

Extended Capabilities

C/C++ Code Generation Generate C & C++ code using MATLAB® Coder™.

Usage notes and limitations:

The input arguments must be constants.

GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

See Also

int2str | mat2str | sprintf | sprintf | str2double | str2num | string

Introduced before R2006a

xuất hiện Example

You have sầu a modified version of this example. Do you want khổng lồ open this example with your edits?

No, overwrite the modified version Yes

MATLAB Command

You clicked a link that corresponds khổng lồ this MATLAB command:

Run the commvà by entering it in the MATLAB Command Window. Web browsers vì not support MATLAB commands.

Cthua thảm

Select a Web Site

Choose a web site khổng lồ get translated nội dung where available & see local events and offers. Based on your location, we recommover that you select: .

Select web site

You can also select a website site from the following list:

How to Get Best Site Performance

Select the Trung Quốc site (in Chinese or English) for best site performance. Other hjwitteveen.com country sites are not optimized for visits from your location.




Asia Pacific


Contact your local office

MATLAB Documentation



Introducing Deep Learning with MATLABDownload ebook


Accelerating the pace of engineering & science

hjwitteveen.com is the leading developer of mathematical computing software for engineers and scientists.

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *