[轉]matlab fprintf 中%tx %f %e


最近在搞一個項目用到浮點運算,做仿真對比時,看到fprintf(fid,'%tx'),一下就懵圈了,查了很多資料都沒講,幸遇以下文章,講的很詳細。

【轉自】http://blog.sina.com.cn/s/blog_50363a790100x2tn.html

MATLAB中將數據寫入文本文件的函數fprintf

  (2012-03-27 08:17:57)
標簽: 

雜談

 
分類: MATLAB

 

函數格式:fprintf(fileIDformatA, ...);fprintf(formatA, ...);count = fprintf(...)

fprintf(fileIDformatA, ...) 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(formatA, ...) 格式化數據並將其結果顯示在屏幕上。

count = fprintf(...) 返回fprintf寫的字節個數。

Input Arguments

fileID

One of the following:

  • An integer file identifier obtained from fopen.

  • 1 for standard output (the screen).

  • 2 for standard error.

Default: 1 (the screen)

format

String in single quotation marks that describes the format of the output fields. Can include combinations of the following:

  • Percent sign followed by a conversion character, such as '%s' for strings.

  • Operators that describe field width, precision, and other options.

  • Literal text to print.

  • Escape characters, including:

    ''

    Single quotation mark

    %%

    Percent character

    \\

    Backslash

    \a

    Alarm

    \b

    Backspace

    \f

    Form feed

    \n

    New line

    \r

    Carriage return

    \t

    Horizontal tab

    \v

    Vertical tab

    \xN

    Hexadecimal number,N

    \N

    Octal number, N

Conversion characters and optional operators appear in the following order (includes spaces for clarity):

The following table lists the available conversion characters and subtypes.

Value Type Conversion Details

Integer, signed

%d or %i

Base 10 values

%ld or %li

64-bit base 10 values

%hd or %hi

16-bit base 10 values

Integer, unsigned

%u

Base 10

%o

Base 8 (octal)

%x

Base 16 (hexadecimal), lowercase letters af

%X

Same as %x, uppercase letters AF

%lu
%lo
%lx or %lX

64-bit values, base 10, 8, or 16

%hu
%ho
%hx or %hX

16-bit values, base 10, 8, or 16

Floating-point number

%f

Fixed-point notation

%e

Exponential notation, such as3.141593e+00

%E

Same as %e, but uppercase, such as3.141593E+00

%g

The more compact of %e or %f, with no trailing zeros

%G

The more compact of %E or %f, with no trailing zeros

%bx or %bX
%bo
%bu

Double-precision hexadecimal, octal, or decimal value
Example: %bx prints pi as 400921fb54442d18

%tx or %tX
%to
%tu

Single-precision hexadecimal, octal, or decimal value
Example: %tx prints pi as 40490fdb

Characters

%c

Single character

%s

String of characters

Additional operators include:

  • Field width

    Minimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('d', intmax) is equivalent to ('%*d', 12, intmax).

  • Precision

    For %f%e, or %E:

    Number of digits to the right of the decimal point.
    Example: '%6.4f' prints pi as '3.1416'

    For %g or %G

    Number of significant digits.
    Example: '%6.4g' prints pi as ' 3.142'

    Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%6.4f', pi) is equivalent to ('%*.*f', 6, 4, pi).

  • Flags

    Action

    Flag

    Example

    Left-justify.

    '–'

    %-5.2f

    Print sign character (+ or ).

    '+'

    %+5.2f

    Insert a space before the value.

    ' '

    % 5.2f

    Pad with zeros.

    '0'

    .2f

    Modify selected numeric conversions:

    • For %o%x, or %X, print 00x, or 0X prefix.

    • For %f%e, or %E, print decimal point even when precision is 0.

    • For %g or %G, do not remove trailing zeros or decimal point.

    '#'

    %#5.0f

  • Identifier

    Order for processing inputs. Use the syntax n$, where n represents the position of the value in the input list.

    For example, '%3$s %2$s %1$s %2$s' prints inputs 'A''B''C' as follows: C B A B.

The following limitations apply to conversions:

  • Numeric conversions print only the real component of complex numbers.

  • If you apply an integer or string conversion to a numeric value that contains a fraction, MATLAB overrides the specified conversion, and uses %e.

  • If you apply a string conversion (%s) to integer values, MATLAB converts values that correspond to valid character codes to characters. For example, '%s'converts [65 66 67] to ABC.

  • Different platforms display exponential notation (such as %e) with a different number of digits in the exponent.

    Platform

    Example

    Windows

    1.23e+004

    UNIX

    1.23e+04
  • Different platforms display negative zero (-0) differently.

     

    Conversion Character

    Platform

    %e or %E

    %f

    %g or %G

    Windows

    0.000000e+000

    0.000000

    0

    Others

    -0.000000e+00

    -0.000000

    -0

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
MATLAB中將數據寫入文本文件的函數fprintf
 

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));
MATLAB中將數據寫入文本文件的函數fprintf
 

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);
MATLAB中將數據寫入文本文件的函數fprintf
 

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
MATLAB中將數據寫入文本文件的函數fprintf
 

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM