Вы находитесь на странице: 1из 6

2/3/2014

fprintf (MATLAB Functions)

MATLAB Function Reference

fprintf
Write formatted data to file

Syntax
c o u n t=f p r i n t f ( f i d , f o r m a t , A , . . . )

Description
c o u n t=f p r i n t f ( f i d , f o r m a t , A , . . . ) formats the data in the real part of matrix A(and

in any additional matrix arguments) under control of the specified f o r m a tstring, and writes it to the file associated with file identifier f i d .f p r i n t freturns a count of the number of bytes written. Argument f i dis an integer file identifier obtained from f o p e n . (It may also be 1for standard output (the screen) or 2for standard error. See f o p e n for more information.) Omitting f i dcauses output to appear on the screen. Format String The f o r m a targument is a string containing C language conversion specifications. A conversion specification controls the notation, alignment, significant digits, field width, and other aspects of output format. The format string can contain escape characters to represent non-printing characters such as newline characters and tabs. Conversion specifications begin with the %character and contain these optional and required elements: Flags (optional) Width and precision fields (optional) A subtype specifier (optional) Conversion character (required) You specify these elements in the following order:

http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fprintf.html

1/6

2/3/2014

fprintf (MATLAB Functions)

Flags You can control the alignment of the output using any of these optional flags. Character Description Example

5 . 2 d A minus sign () Left-justifies the converted argument in its field. %

A plus sign (+ ) Zero (0 )

Always prints a sign character (+ or -). Pad with zeros rather than spaces.

% + 5 . 2 d % 0 5 . 2 d

Field Width and Precision Specifications You can control the width and precision of the output by including these options in the format string. Character Description Field width A digit string specifying the minimum number of digits to be printed. Precision Example
% 6 f

6 . 2 f A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point. %

Conversion Characters Conversion characters specify the notation of the output. Specifier Description
% c % d

Single character Decimal notation (signed)


2/6

http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fprintf.html

2/3/2014

fprintf (MATLAB Functions)

% e % E % f % g % G % i % o % s % u % x % X

Exponential notation (using a lowercase eas in 3 . 1 4 1 5 e + 0 0 ) Exponential notation (using an uppercase Eas in 3 . 1 4 1 5 E + 0 0 ) Fixed-point notation The more compact of % eor % f , as defined in [2]. Insignificant zeros do not print. Same as % g , but using an uppercase E Decimal notation (signed) Octal notation (unsigned) String of characters Decimal notation (unsigned) Hexadecimal notation (using lowercase letters a -f ) Hexadecimal notation (using uppercase letters A -F )

Conversion characters % o ,% u ,% x , and % Xsupport subtype specifiers. See Remarks for more information. Escape Characters This table lists the escape character sequences you use to specify non-printing characters in a format specification. Character
\ b \ f \ n \ r \ t \ \

Description Backspace Form feed New line Carriage return Horizontal tab Backslash
3/6

http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fprintf.html

2/3/2014

fprintf (MATLAB Functions)

\'' or '' (two single quotes)


% %

Single quotation mark

Percent character

Remarks
The f p r i n t ffunction behaves like its ANSI C language namesake with these exceptions and extensions. If you use f p r i n t fto convert a MATLAB double into an integer, and the double contains a value that cannot be represented as an integer (for example, it contains a fraction), MATLAB ignores the specified conversion and outputs the value in exponential format. To successfully perform this conversion, use the f i x ,f l o o r ,c e i l , or r o u n dfunctions to change the value in the double into a value that can be represented as an integer before passing it to s p r i n t f . The following, non-standard subtype specifiers are supported for the conversion characters % o ,% u ,% x , and % X .
b

The underlying C data type is a double rather than an unsigned integer. For example, to print a double-precision value in hexadecimal, use a format like '% b x '.

t The underlying C data type is a float rather than an unsigned integer. For example, to print a double value in hexadecimal use the format ' % b x ' The f p r i n t ffunction is vectorized for nonscalar arguments. The function recycles the format string through the elements of A(columnwise) until all the elements are used up. The function then continues in a similar manner through any additional matrix arguments. Note
f p r i n t fdisplays negative zero

(-0) differently on some platforms, as shown in the following table.

Conversion Character Platform %e or %E PC SGI %f %g or %G 0 0


4/6

0.000000e+000 0.000000 0.000000e+00 0.000000

http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fprintf.html

2/3/2014

fprintf (MATLAB Functions)

HP700 Others

-0.000000e+00 -0.000000 0 -0.000000e+00 -0.000000 -0

Examples
The statements
x=0 : . 1 : 1 ; y=[ x ;e x p ( x ) ] ; f i d=f o p e n ( ' e x p . t x t ' , ' w ' ) ; f p r i n t f ( f i d , ' % 6 . 2 f% 1 2 . 8 f \ n ' , y ) ; f c l o s e ( f i d )

create a text file called e x p . t x tcontaining a short table of the exponential function:
0 . 0 0 0 . 1 0 . . . 1 . 0 0 1 . 0 0 0 0 0 0 0 0 1 . 1 0 5 1 7 0 9 2 2 . 7 1 8 2 8 1 8 3

The command
f p r i n t f ( ' Au n i tc i r c l eh a sc i r c u m f e r e n c e% g . \ n ' , 2 * p i )

displays a line on the screen:


Au n i tc i r c l eh a sc i r c u m f e r e n c e6 . 2 8 3 1 8 6 .

To insert a single quotation mark in a string, use two single quotation marks together. For example,
f p r i n t f ( 1 , ' I t ' ' sF r i d a y . \ n ' )

displays on the screen:


I t ' sF r i d a y .

The commands
B=[ 8 . 8 7 . 7 ;8 8 0 0 7 7 0 0 ]
http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fprintf.html 5/6

2/3/2014

fprintf (MATLAB Functions)

f p r i n t f ( 1 , ' Xi s% 6 . 2 fm e t e r so r% 8 . 3 fm m \ n ' , 9 . 9 , 9 9 0 0 , B )

display the lines:


Xi s9 . 9 0m e t e r so r9 9 0 0 . 0 0 0m m Xi s8 . 8 0m e t e r so r8 8 0 0 . 0 0 0m m Xi s7 . 7 0m e t e r so r7 7 0 0 . 0 0 0m m

Explicitly convert MATLAB double-precision variables to integral values for use with an integral conversion specifier. For instance, to convert signed 32-bit data to hexadecimal format:
a=[ 61 01 44 4 ] ; f p r i n t f ( ' % 9 X \ n ' , a+( a < 0 ) * 2 ^ 3 2 ) 6 A E 2 C

See Also
f c l o s e ,f e r r o r ,f o p e n ,f r e a d ,f s c a n f ,f s e e k ,f t e l l ,f w r i t e ,d i s p

References
[1] Kernighan, B.W. and D.M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988. [2] ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.

fplot

fprintf (serial)

http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fprintf.html

6/6

Вам также может понравиться