📅  最后修改于: 2021-01-07 02:09:06             🧑  作者: Mango
数据以数组形式保存。当我们显示数据时,它以数组的形式出现,并且可能不像以前那样可表示。因此,MATLAB中提供了一些格式化运算符,可用于将数据转换为文本并根据我们的要求格式化输出。
>> % create a row vector using rand function
>> r = rand(1,3);
>> % combining normal text, special character, formatting operator in the output
>> t = sprintf('Displaying 3 random numbers: \n 1)%f \n 2)%.2f \n 3)%12f',r);
输出:
>> t
t =
'Displaying 3 random numbers:
1)0.957167
2)0.49
3)0.800280'
%百分号始终引领格式运算符。格式运算符最多可以有六个字段-转换字符,子类型,精度,字段宽度,标志和数字标识符。
语法示例:
转换字符是唯一必需的字段,它指定输出的符号。转换字符由单个字母字符表示,并出现在格式说明符的最后。
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) |
例:
>>p=100;
>>ch = 'cdeEfgGosuxX';
>>res = sprintf("%c\t\t%c \n%c\t\t%d \n%c\t\t%e \n%c\t\t%E"+...
"\n%c\t\t%f \n%c\t\t%g \n%c\t\t%G \n%c\t\t%o"+...
"\n%c\t\t%s\n%c\t\t%u\n%c\t\t%x\n%c\t\t%X?, ...
ch(1),p,ch(2),p,+ch(3),p,ch(4),p,ch(5),p,ch(6),+...
p,ch(7),p,ch(8),p,ch(9),p,ch(10),p,ch(11),p,ch(12),p);
输出:
>> res
res =
"c d
d 100
e 1.000000e+02
E 1.000000E+02
f 100.000000
g 100
G 100
o 144
s d
u 100
x 64
X 64"
子类型字段由紧接转换字符之前的单个字母字符表示。转换字符%o,%u,%x和%X将输入的数据视为整数,而没有子类型字段。因此,使用以下子类型说明符之一将数据输入视为浮点值,并将它们转换为八进制,十进制或十六进制表示形式。
子类型说明符描述b将输入数据视为双精度浮点值,而不是无符号整数。 t将输入数据视为单精度浮点值,而不是无符号整数。
例:
>> p = 100;
>> res = sprintf('before using subtype:\t%u\nafter using subtype:\t%tu',p,p);
输出:
>> res
res =
'before using subtype: 100
after using subtype: 1120403456'
精度字段由一个非负整数表示,该整数在一个句点(点)之后立即出现。它与%f,%e和%E运算符,并指示要显示在小数点右边的位数。
例:
>> res = sprintf('output without and with precision field:\t%f\t%.2f\t%e\t%.2e',pi*30*ones(1,4));
输出:
>> res
res =
'output without and with precision field: 94.247780 94.25 9.424778e+01 9.42e+01'
字段宽度是一个非负整数,带有或不带有小数点精度。由于精度字段指定小数点后的位数,因此字段宽度指定应在输出中显示多少个总字符。如果字段宽度大于输入数据中的字符数,那么默认情况下,输出文本将用空格字符填充。
例:
>> res = sprintf('output without and with field width:\t|%x|\t|%15x|\t|%f|\t|%15f|',303.3*ones(1,4));
输出:
>> res
res =
'output without and with field width: |3.033000e+02| | 3.033000e+02| |303.300000| | 303.300000|'
标志字段控制输出的其他格式。用于标志的字符主要描述间距,填充和文本对齐。
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:
|
%#5.0f |
示例:-左右对齐:
>> sprintf('right-justify:|%+12s|\nleft-justify:|%-12.2f|',"flags",10);
输出:
>> ans
ans =
'right-justify:| flags|
left-justify:|10.00 |'
示例:-用空格和零填充:
>> b = sprintf('padding with space: |%10.2f|\n padding with zero: |%010.2f|',20,20);
输出:
>> b
b =
'padding with space: | 20.00|
padding with zero: |0000020.00|'
示例:-磅符号:
>> a=sprintf('%#.0f',10.00);
输出:
>> a
a =
'10.'
诸如sprintf之类的输出函数以与接受输入参数相同的顺序打印输出。因此,使用标识符以自定义指定顺序生成输出。标识符是整数值,紧跟在百分号之后,后跟一个美元符号。
例:
>> t1 = sprintf('%s %s %s','1st','2nd','3rd');
输出:
>> t1
t1 =
'1st 2nd 3rd'
例:
>> t2 = sprintf('%3$s %1$s %2$s','1st','2nd','3rd');
输出:
>> t2
t2 =
'3rd 1st 2nd'
特殊字符用于特定目的,因此不能用作普通文本。要将特殊字符作为输出文本的一部分,请使用特定的字符序列。
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 ‘@’ |
格式运算符遵循特定的格式来格式化输出文本。字段宽度和精度定义了如何在输出中显示数字。让我们来看一个示例:
要在格式说明符之外指定字段宽度和精度,请使用星号(*)代替格式运算符的字段宽度或精度字段。星号的位置应与在输入参数中指定字段的数字的位置匹配。让我们看一个例子:
例:
>> t3 = sprintf('%*f %.*f %*.*f',10,123.456,3,10.2345,4,2,pi);
输出:
>> t3
t3 =
?123.456000 10.235 3.14?
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. |
我们可以为字段宽度和精度指定编号的标识符,并且标识符的值来自输入参数。
例:
>> t4 = sprintf('%1$*4$f %2$.*5$f %3$*6$.*7$f',123.456, 12.36587, 3.145678, 10, 4, 4, 2);
输出:
>>t4 =
'123.456000 12.3659 3.14'
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 |