matlab sprintf 数字转成字符串

本文深入解析了sprintf函数的用法,包括如何将数据格式化为字符串、输入参数、输出结果以及常见例子。通过实际操作演示,帮助读者掌握sprintf函数的基本应用。

sprintf


Format data into string


Syntax


str = sprintf(format, A, ...)
[str, errmsg] = sprintf(format, A, ...)

Description


str = sprintf(format, A, ...) applies the format to all elements of array A and any additional array arguments in column order, and returns the results to string str.

[str, errmsg] = sprintf(format, A, ...) returns an error message string when the operation is unsuccessful. Otherwise, errmsg is empty.

Input Arguments


Examples

Example 1


Format floating-point numbers:
sprintf('%0.5f',1/eps)   % 4503599627370496.00000
sprintf('%0.5g',1/eps)   % 4.5036e+15


Example 2


Explicitly convert double-precision values to integers:
sprintf('%d',round(pi))   % 3

Example 3


Combine literal text with array values:
sprintf('The array is %dx%d.',2,3) % The array is 2x3

Example 4


On a Windows system, convert PC-style exponential notation (three digits in the exponent) to UNIX style notation (two digits):
a = sprintf('%e', 12345.678);
if ispc
   a = strrep(a, 'e+0', 'e+');
end

Example 5


This example demonstrates the use of the width field of a format specifier to set the minimum width of the result. This is different from the sscanf function where this field represents the maximum width of the result.

The first statement uses a format of %6d, which is equal to the number of digits in the input value. sprintf returns a single value as the result:
N = sprintf('%6d', [123456])
N =
123456


The second statement passes a format specifier of %025d, in which the width field of 25 determines the minimum width of the return value:
N = sprintf('%025d', [123456])
N =
0000000000000000000123456


The 0 in the %025d format specifier indicates that you want leading zeros to be shown in the output.

Input Arguments

fileID

One ofthe following:

  • An integer file identifier obtained from fopen.

  • 1 for standard output (the screen).

  • 2 for standard error.

Default: 1 (the screen)

format

String in single quotation marks that describes the format ofthe output fields. Can include combinations of the following:

  • Percent sign followed by a conversion character, suchas '%s' for strings.

  • Operators that describe field width, precision, andother options.

  • Literal text to print.

  • Escape characters, including:

    ''

    Single quotation mark

    %%

    Percent character

    \\

    Backslash

    \a

    Alarm

    \b

    Backspace

    \f

    Form feed

    \n

    New line

    \r

    Carriage return

    \t

    Horizontal tab

    \v

    Vertical tab

    \xN

    Hexadecimal number, N

    \N

    Octal number, N

Conversion characters and optional operators appear in the followingorder (includes spaces for clarity):

The following table lists the available conversion charactersand subtypes.

Value TypeConversionDetails

Integer, signed

%d or %i

Base 10

Integer, unsigned

%u

Base 10

%o

Base 8 (octal)

%x

Base 16 (hexadecimal), lowercase letters af

%X

Same as %x, uppercase letters AF

Floating-point number

%f

Fixed-point notation

%e

Exponential notation, such as 3.141593e+00

%E

Same as %e, but uppercase, such as 3.141593E+00

%g

The more compact of %e or %f,with no trailing zeros

%G

The more compact of %E or %f,with no trailing zeros

%bx or %bX
%bo
%bu

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

%tx or %tX
%to
%tu

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

Characters

%c

Single character

%s

String of characters


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值