最近在搞一個項目用到浮點運算,做仿真對比時,看到fprintf(fid,'%tx'),一下就懵圈了,查了很多資料都沒講,幸遇以下文章,講的很詳細。
【轉自】http://blog.sina.com.cn/s/blog_50363a790100x2tn.html
MATLAB中將數據寫入文本文件的函數fprintf

函數格式:fprintf(fileID, format, A, ...);fprintf(format, A, ...);count = fprintf(...)
fprintf(fileID, format, A, ...) applies the format to all elements of array A and any additional array arguments in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen.
fprintf(format, A, ...) 格式化數據並將其結果顯示在屏幕上。
count = fprintf(...) 返回fprintf寫的字節個數。
Input Arguments
fileID |
One of the following:
Default: 1 (the screen) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format |
String in single quotation marks that describes the format of the output fields. Can include combinations of the following:
Conversion characters and optional operators appear in the following order (includes spaces for clarity): The following table lists the available conversion characters and subtypes.
Additional operators include:
The following limitations apply to conversions:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A |
Numeric or character array. |
Examples
Print multiple values and literal text to the screen:
B = [8.8 7.7 ; ... 8800 7700]; fprintf('X is %4.2f meters or %8.3f mm\n', 9.9, 9900, B)
MATLAB displays:
X is 9.90 meters or 9900.000 mm X is 8.80 meters or 8800.000 mm X is 7.70 meters or 7700.000 mm
![]() |
Explicitly convert double-precision values with fractions to integer values, and print to the screen:
a = [1.02 3.04 5.06]; fprintf('%d\n', round(a));
![]() |
Write a short table of the exponential function to a text file called exp.txt:
x = 0:.1:1; y = [x; exp(x)]; % open the file with write permission fid = fopen('exp.txt', 'w'); fprintf(fid, '%6.2f .8f\n', y); fclose(fid); % view the contents of the file type exp.txt
MATLAB import functions, all UNIX applications, and Microsoft Word and WordPad recognize '\n' as a newline indicator. However, if you plan to read the file with Microsoft Notepad, use '\r\n' to move to a new line when writing. For example, replace the previous call to fprintf with the following:
fprintf(fid, '%6.2f .8f\r\n', y);
![]() |
On a Windows system, convert PC-style exponential notation (three digits in the exponent) to UNIX-style notation (two digits), and print data to a file:
a = [0.06 0.1 5 300] % use sprintf to convert the numeric data to text, using %e a_str = sprintf('%e\t',a) % use strrep to replace exponent prefix with shorter version a_str = strrep(a_str,'e+0','e+'); a_str = strrep(a_str,'e-0','e-'); % call fprintf to print the updated text strings fid = fopen('newfile.txt','w'); fprintf(fid, '%s', a_str); fclose(fid); % view the contents of the file type newfile.txt
![]() |
Display a hyperlink (The MathWorks Web Site) on the screen:
site = 'http://www.mathworks.com'; title = 'The MathWorks Web Site'; fprintf('<a href = "%s">%s</a>\n', site, title)