matlab清除数据格式化,MATLAB格式化文本

格式化文字

数据以数组形式保存。当我们显示数据时,它以数组的形式出现,并且可能不像以前那样可表示。因此,MATLAB中提供了一些格式化运算符,可用于将数据转换为文本并根据我们的要求格式化输出。

格式运算符控制输出的符号,对齐方式,有效数字。

我们可以在格式说明符中将格式运算符与普通文本和特殊字符结合使用。

格式化运算符可以与以下函数一起使用:compose,num2str,sprintf,fprintf,assert,error,warning和MException。

例:

输出:

MATLAB中格式运算符的字段

%百分号始终引领格式运算符。格式运算符最多可以有六个字段-转换字符,子类型,精度,字段宽度,标志和数字标识符。

转换字符是所有六个字段中唯一需要的字段。

转换字符必须始终以%百分号开头。

并且在格式运算符的字段之间不允许有空格字符。

语法示例:

matlab-formatting-text.png

转换字符

转换字符是唯一必需的字段,它指定输出的符号。转换字符由单个字母字符表示,并出现在格式说明符的最后。

Sl. No.

Conversion character

Description

1.

c

Single character

2.

d

Decimal notation (signed)

3.

e

Exponential notation (using a lowercase e, as in 3.1415e+00)

4.

E

Exponential notations (using a uppercase E, as in 3.1415E+00)

5.

f

Fixed-point notation

6.

g

The more compact of the %e or %f (insignificant zero do not print)

7.

G

Similar to %g, but using an uppercase E

8.

o

Octal notation (unsigned)

9.

s

Character vector or string array

10.

u

Decimal notation (unsigned)

11.

x

Hexadecimal notation (unsigned, using lowercase letters a-f)

12.

X

Hexadecimal notation (unsigned, using lowercase letters A-F)

例:

输出:

亚型

子类型字段由紧接转换字符之前的单个字母字符表示。转换字符%o,%u,%x和%X将输入的数据视为整数,而没有子类型字段。因此,使用以下子类型说明符之一将数据输入视为浮点值,并将它们转换为八进制,十进制或十六进制表示形式。

子类型说明符描述b将输入数据视为双精度浮点值,而不是无符号整数。 t将输入数据视为单精度浮点值,而不是无符号整数。

例:

输出:

精确

精度字段由一个非负整数表示,该整数在一个句点(点)之后立即出现。它与%f,%e和%E运算符,并指示要显示在小数点右边的位数。

例:

输出:

场宽

字段宽度是一个非负整数,带有或不带有小数点精度。由于精度字段指定小数点后的位数,因此字段宽度指定应在输出中显示多少个总字符。如果字段宽度大于输入数据中的字符数,那么默认情况下,输出文本将用空格字符填充。

例:

输出:

标志

标志字段控制输出的其他格式。用于标志的字符主要描述间距,填充和文本对齐。

Flags character

Description

Example

Minus sign (-)

Left-justify the output

%-5.2d

Plus sign (+)

Right-justify the output if the output is a text, and add a leading plus sign character if it is number

%+5.2d

%+5s

Space

Inserts space before the output

% 5.2f

Zero (0)

Pad the output with zero instead of space

%05.2f

The pound sign (#)

The pound sign flag is a special case as it is somehow different from other flags. It modifies selected numeric conversions as:

Prints 0, 0x, or 0X prefix for %o, %x, or %X operators.

Prints decimal point even when precision is 0 for %f, %e, or %E operators.

It doesn’t remove trailing zeroes or decimal points for %g or %G operators.

%#5.0f

示例:-左右对齐:

输出:

示例:-用空格和零填充:

输出:

示例:-磅符号:

输出:

身份标识

诸如sprintf之类的输出函数以与接受输入参数相同的顺序打印输出。因此,使用标识符以自定义指定顺序生成输出。标识符是整数值,紧跟在百分号之后,后跟一个美元符号。

默认顺序

例:

输出:

使用标识符的自定义订单

例:

输出:

在输出中显示特殊字符

特殊字符用于特定目的,因此不能用作普通文本。要将特殊字符作为输出文本的一部分,请使用特定的字符序列。

Special character

Character sequence

Single quotation mark

” (single quote two times)

%%

Backslash

\\

Alarm

\a

Backspace

\b

Form feed

\f

New line

\n

Carriage return

\r

Horizontal tab

\t

Vertical tab

\v

Representation of Unicode numeric value of the hexadecimal number, N

\xN

Example: sprintf(‘\x6A’) returns character ‘j’

Representation of Unicode numeric value of the octal number, N

\N

Example: sprintf(‘\100’) returns character ‘@’

设置字段宽度和精度的规则

格式运算符遵循特定的格式来格式化输出文本。字段宽度和精度定义了如何在输出中显示数字。让我们来看一个示例:

matlab-formatting-text2.png

如果未指定precision,则默认值设置为6。

如果精度p小于输入小数部分中的一位数字,则小数点后仅显示p位数字,并且输出值四舍五入。

如果精度p高于否。取小数部分的位数,则显示小数部分,并在p位数的右边添加零。

如果未指定字段宽度w,则根据p + 1 + n计算默认值,n是输入值整个部分的位数。

如果字段宽度w大于p + 1 + n,则将输出的整个部分用空格或零个附加字符填充到左侧,以w-(p + 1 + n)计算。

字段宽度和精度,格式说明符之外

要在格式说明符之外指定字段宽度和精度,请使用星号(*)代替格式运算符的字段宽度或精度字段。星号的位置应与在输入参数中指定字段的数字的位置匹配。让我们看一个例子:

例:

输出:

上面例子的解释:

Formatting Operator

Description

%*f

Specify field width as the following input argument, 10.

%.*f

Specify precision as the next input argument, 3.

%*.*f

Specify width and precision as the next input arguments, 4, and 2.

指定标识符代替场宽和精度

我们可以为字段宽度和精度指定编号的标识符,并且标识符的值来自输入参数。

matlab-formatting-text3.png

例:

输出:

让我们来看一下上面的例子:

Formatting operator

Description

%1$*4$f

1$ specifies the first input argument, 123.456, as the value

*4$ specifies the fourth input argument, 10, as the field width

%2$.*5$f

2$ specifies the second input argument, 12.36587, as the value

.*5$ specifies the fifth input argument, 4, as the precision

%3$*6$.*7$f

3$ specifies the third input argument, 3.145678, as the value

*6$ specifies the sixth input argument, 4, as the field width

.*7$ specifies the seventh input argument, 2, as the precision

0

相关文章:MATLAB教程 MATLAB教程 MATLAB是MathWorks开发的一种编程语言。它最初是一种矩阵编程语言,线性代数编程很 […]...

MATLAB-M文件 MATLAB-M文件 到目前ߒ […]...

MATLAB-数组 MATLAB-数组 MATLAB中所有& […]...

MATLAB-冒号表示法 MATLAB-冒号表示法 Colon […]...

MATLAB-差分 MATLAB-微分 MATLAB提供了d […]...

MATLAB-集成 MATLAB-整合 集成处理 […]...

如何安装MATLAB MATLAB安装 MATLAB安装 第1步:双击MATLAB图标(我们先前下载的二进制文件)。单击图标后,将弹出一个对话框,要求 […]...

MATLAB Online MATLAB在线 MATLAB Online是在网络浏览器中运行的MATLAB的在线版本。使用MATLAB之前 […]...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值