Skip to content

fprintf

Write formatted data to a file or the console.

fprintf(format, A, ...)
fprintf(fid, format, A, ...)
[count, errmsg] = fprintf(...)

Formats arguments with C-style format specifiers (%d, %f, %s, etc.) and writes the result. Without fid, writes to the console. With a fid from fopen, writes to that file. Two-output form returns the byte count and any error message.

fprintf('Result: %d\n', 42)
fid = fopen('log.txt', 'a'); fprintf(fid, '%s\n', msg); fclose(fid);
  • sprintf — Format data into a string.
  • csvwrite — Write a matrix to a CSV file.