clear;
fid=fopen('answers.txt','w');
fprintf(fid,'My Name is Qiaofeng, I am from China. \r\n');
fprintf(fid,'I travelled a lot. \r\n');
for i=1:3
j=3*i;
fprintf(fid,'When I was %u years old, I went to City %u . \r\n',j,i);
end
status=fclose(fid);
输出
My Name is Qiaofeng, I am from China.
I travelled a lot.
When I was 3 years old, I went to City 1 .
When I was 6 years old, I went to City 2 .
When I was 9 years old, I went to City 3 .
(zz)
"\r"指的是“回车”符号,即回到本行的开头,
"\n"指的是“换行”符号,即开始新的一行,
根据我的使用经验,在fprintf函数中两者结合使用,
才能达到在新的一行的开头开始打印的目的。
附:matlab输出格式,(from www.mathworks.com)
%6.2f表示输出数字小数点左六位,右两位
例2:
n =
984.1240
fprintf(myfid,'n= %6.1f\r\n n=%2.6f\r\n')
输出如下
n= 984.1
n=984.124000
| Character | Description | Example |
|---|---|---|
| Field width | A string specifying the minimum number of characters to be printed. This includes a plus or minus sign, any leading zeros, numeric digits, and decimal point. | %6f |
| Precision | A string including a period (.) specifying the number of digits to be printed to the right of the decimal point | %6.2f |
Conversion Characters
Conversion characters specify the notation of the output.| Specifier | Description |
|---|---|
| %c | Single character |
| %d | Decimal notation (signed) |
| %e | Exponential notation (using a lowercase e as in 3.1415e+00) |
| %E | Exponential notation (using an uppercase E as in 3.1415E+00) |
| %f | Fixed-point notation |
| %g | The more compact of %e or %f, as defined in [2]. Insignificant zeros do not print. |
| %G | Same as %g, but using an uppercase E |
| %o | Octal notation (unsigned) |
| %s | String of characters |
| %u | Decimal notation (unsigned) |
| %x | Hexadecimal notation (using lowercase letters a–f) |
| %X | Hexadecimal notation (using uppercase letters A–F) |
Escape Characters
This table lists the escape character sequences you use to specify non-printing characters in a format specification.
| Character | Description |
|---|---|
| \b | Backspace |
| \f | Form feed |
| \n | New line |
| \r | Carriage return |
| \t | Horizontal tab |
| \\ | Backslash |
| \'' (two single quotes) | Single quotation mark |
| %% | Percent character |
没有评论:
发表评论