
(It may also be 1 for standard output (the screen) or 2 for standard error. Argument fid is an integer file identifier obtained from fopen. fprintf returns a count of the number of bytes written. ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.Fprintf (MATLAB Function Reference) MATLAB Function Referenceĭescription count = fprintf(fid, format,A.)įormats the data in the real part of matrix A (and in any additional matrix arguments) under control of the specified format string, and writes it to the file associated with file identifier fid. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988. See Also fclose, ferror, fopen, fread, fscanf, fseek, ftell, fwrite For instance, to convert signed 32-bit data to hexadecimal format: For example,įprintf(1,'X is %6.2f meters or %8.3f mm\n',9.9,9900,B)Įxplicitly convert MATLAB double-precision variables to integral values for use with an integral conversion specifier. To insert a single quotation mark in a string, use two single quotation marks together. Left-justifies the converted argument in its field.Ī digit string that specifies the minimum number of digits to be printed.Ī digit string including a period (.) that specifies the number of digits to be printed to the right of the decimal point.įor more information about format strings, refer to the printf() and fprintf() routines in the documents listed in "References".Ĭreate a text file called exp.txt containing a short table of the exponential function:įprintf('A unit circle has circumference %g.\n',2*pi)Ī unit circle has circumference 6.283186.

Other Characters Other characters can be inserted into the conversion specifier between the % and the conversion character. Hexadecimal notation (using uppercase letters A- F)

Hexadecimal notation (using lowercase letters a- f) The more compact of %e or %f, as defined in insignificant zeros do not print The following tables describe the nonalphanumeric characters found in format specification strings.Ĭonversion Specifiers Conversion characters specify the notation of the output.Įxponential notation (using a lowercase e as in 3.1415e+00)Įxponential notation (using an uppercase E as in 3.1415E+00) It is then cycled in a similar manner, without reinitializing, through any additional matrix arguments. The format string is cycled through the elements of A (columnwise) until all the elements are used up. When input matrix A is nonscalar, fprintf is vectorized. The underlying C data type is a float rather than an unsigned integer. For example, to print a double-precision value in hexadecimal, use a format like ' %bx'. The underlying C data type is a double rather than an unsigned integer.

These non-standard subtype specifiers are supported for conversion specifiers %o, %u, %x, and %X. Remarks The fprintf function behaves like its ANSI C language fprintf() namesake with certain exceptions and extensions, including: It can contain ordinary alphanumeric characters, along with escape characters, conversion specifiers, and other characters, organized as shown below. The format string specifies notation, alignment, significant digits, field width, and other aspects of output format. See fopen for more information.) Omitting fid from fprintf's argument list causes output to appear on the screen, and is the same as writing to standard output ( fid = 1).

Fprintf (MATLAB Function Reference) MATLAB Function Referenceĭescription count = fprintf(fid,format,A.)įormats the data in the real part of matrix A (and in any additional matrix arguments) under control of the specified format string, and writes it to the file associated with file identifier fid.
