4/20/2008

matlab中输出结果到文件的新方法

例子
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

CharacterDescriptionExample
Field widthA 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
PrecisionA 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.
SpecifierDescription
%cSingle character
%dDecimal notation (signed)
%eExponential notation (using a lowercase e as in 3.1415e+00)
%EExponential 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.
%GSame as %g, but using an uppercase E
%oOctal notation (unsigned)
%sString of characters
%uDecimal notation (unsigned)
%xHexadecimal notation (using lowercase letters af)
%XHexadecimal notation (using uppercase letters AF)
The following tables describe the nonalphanumeric characters found in format specification strings.

Escape Characters


This table lists the escape character sequences you use to specify non-printing characters in a format specification.
CharacterDescription
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\\ Backslash
\''
(two single quotes)
Single quotation mark
%% Percent character

没有评论: