fprintf
Writes formatted text to the command window or to a file.
Category: Data Import and Analysis > Data Import and Export
Syntax
fprintf(formatSpec, A1, ..., An)
Prints to the command window.
fprintf(fid, formatSpec, A1, ..., An)
Writes to the open file fid. Identifier 1 is the command window.
nbytes = fprintf(...)
Returns the number of bytes written.
Description
The format is applied repeatedly until all values are used, so fprintf('%d, ', v) prints a whole vector. If the values run out in the middle of a format, output stops just before the first conversion that has no data. A %d given a non-integer value switches to exponent notation instead of truncating.
fprintf does not add a newline: end the format with \n when you want one.
Input arguments
formatSpec- Format text with conversions such as
%d(integer),%fand%.2f(fixed point),%e(exponent),%g(shortest),%s(text),%c(character) and%%. Escapes like\nand\tare interpreted. A1, ..., An- Values to insert. Arrays are consumed element by element, in column order.
Output arguments
nbytes- Number of bytes written.
Example
Formats and recycling
fprintf('%5.2f|%d|%s\n', pi, 42, 'abc');
fprintf('%d, ', [1 2 3]); fprintf('\n');
fprintf('%d %s\n', 1, 'a', 2, 'b');
fprintf('%x %o %c %%\n', 255, 8, 'A');
fprintf('%-6d|%+d\n', 5, 5);
Output
3.14|42|abc
1, 2, 3,
1 a
2 b
ff 10 A %
5 |+5