fprintf - Write data to text file - MATLAB (original) (raw)

Syntax

Description

fprintf([fileID](#btf8xsy-1-fileID),[formatSpec](#btf8xsy-1%5Fsep%5Fshared-formatSpec),[A1,...,An](#btf8xsy-1-A1An)) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen.

example

fprintf([formatSpec](#btf8xsy-1%5Fsep%5Fshared-formatSpec),[A1,...,An](#btf8xsy-1-A1An))formats data and displays the results on the screen.

example

[nbytes](#btf8xsy-1-nbytes) = fprintf(___) returns the number of bytes that fprintf writes, using any of the input arguments in the preceding syntaxes.

example

Examples

collapse all

Print multiple numeric values and literal text to the screen.

A1 = [9.9, 9900]; A2 = [8.8, 7.7 ; ... 8800, 7700]; formatSpec = 'X is %4.2f meters or %8.3f mm\n'; fprintf(formatSpec,A1,A2)

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

%4.2f in the formatSpec input specifies that the first value in each line of output is a floating-point number with a field width of four digits, including two digits after the decimal point. %8.3f in the formatSpec input specifies that the second value in each line of output is a floating-point number with a field width of eight digits, including three digits after the decimal point. \n is a control character that starts a new line.

Explicitly convert double-precision values with fractions to integer values.

a = [1.02 3.04 5.06]; fprintf('%d\n',round(a));

%d in the formatSpec input prints each value in the vector, round(a), as a signed integer. \n is a control character that starts a new line.

Write a short table of the exponential function to a text file called exp.txt.

x = 0:.1:1; A = [x; exp(x)];

fileID = fopen('exp.txt','w'); fprintf(fileID,'%6s %12s\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\n',A); fclose(fileID);

The first call to fprintf prints header text x and exp(x), and the second call prints the values from variable A.

If you plan to read the file with Microsoft® Notepad, use '\r\n' instead of '\n' to move to a new line. For example, replace the calls to fprintf with the following:

fprintf(fileID,'%6s %12s\r\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\r\n',A);

MATLAB® import functions, all UNIX® applications, and Microsoft Word and WordPad recognize '\n' as a newline indicator.

View the contents of the file with the type command.

 x       exp(x)

0.00 1.00000000 0.10 1.10517092 0.20 1.22140276 0.30 1.34985881 0.40 1.49182470 0.50 1.64872127 0.60 1.82211880 0.70 2.01375271 0.80 2.22554093 0.90 2.45960311 1.00 2.71828183

Write data to a file and return the number of bytes written.

Write an array of data, A, to a file and get the number of bytes that fprintf writes.

A = magic(4);

fileID = fopen('myfile.txt','w'); nbytes = fprintf(fileID,'%5d %5d %5d %5d\n',A)

The fprintf function wrote 96 bytes to the file.

Close the file.

View the contents of the file with the type command.

16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1

Display a hyperlink (The MathWorks Web Site) on the screen.

url = 'https://www.mathworks.com'; sitename = 'The MathWorks Web Site';

fprintf('%s\n',url,sitename)

%s in the formatSpec input indicates that the values of the variables url and sitename, should be printed as text.

Input Arguments

collapse all

File identifier, specified as one of the following:

Data Types: double

Numeric or character arrays, specified as a scalar, vector, matrix, or multidimensional array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char

Output Arguments

collapse all

Number of bytes that fprintf writes, returned as a scalar. When writing to a file, nbytes is determined by the character encoding. When printing data to the screen, nbytes is the number of characters displayed on the screen.

Tips

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.

Extended Capabilities

expand all

Usage notes and limitations:

The fprintf function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a