fprintf

This function writes output to a file using the printf format_string capabilities. The first argument is a fileID returned from an earlier call to fopen.

Syntax

R = fprintf(fileID, format_string, var1, var2, ...)

Inputs

fileID
File ID returned from an earlier call to fopen.
Type: integer
format_string
The format_string can consist of text to be printed and any number of placeholders for one or more variables.
See printf for information about the use of the format_string.
Type: string

Outputs

R
The number of characters written to the file.
Type: integer

Examples

% Open file for write
printf('Write file contents using fprintf\n')
[fopen_fileID, fopen_msg] = fopen('testfile', 'w');
[fgets_text, fgets_len] = fopen(fopen_fileID); % returns information about file

test = 'test';
fprintf(fopen_fileID, '\n')
fprintf(fopen_fileID, 'print %-8s %-22s **%s**\n', 'string', ' ', test)
fprintf(fopen_fileID, 'print %-8s %-22s **%10s**\n', 'string', 'minimum size', test)
fprintf(fopen_fileID, 'print %-8s %-22s **%-10s**\n', 'string', 'string left justified', test)
fprintf(fopen_fileID, '\n') % print blank line between sections

numtest = 1234568;
fprintf(fopen_fileID, '\n')
fprintf(fopen_fileID, 'print %-8s %-22s **%d**\n', 'number', ' ', numtest)
fprintf(fopen_fileID, 'print %-8s %-22s **%10d**\n', 'number', 'minimum size', numtest)
fprintf(fopen_fileID, 'print %-8s %-22s **%-10d**\n', 'number', 'left justified', numtest)
fprintf(fopen_fileID, 'print %-8s %-22s **%010d**\n', 'number', 'zero fill', numtest)
fprintf(fopen_fileID, '\n') % print blank line between sections

format('long')
floatest = 120 + pi;
fprintf(fopen_fileID, 'print %-8s %-22s **%f**\n', 'float', ' ', floatest)
fprintf(fopen_fileID, 'print %-8s %-22s **%14f**\n', 'float', 'minimum width', floatest)
fprintf(fopen_fileID, 'print %-8s %-22s **%-14f**\n', 'float', 'left justified', floatest)
fprintf(fopen_fileID, 'print %-8s %-22s **%014f**\n', 'float', 'zero filled', floatest)
fprintf(fopen_fileID, 'print %-8s %-22s **%014.3f**\n', 'float', '3 decimal restriction', floatest)
fprintf(fopen_fileID, 'print %-8s %-22s **%014.8f**\n', 'float', '8 decimal expansion', floatest)

fclose(fopen_fileID);

printf('Read back and display contents written using fprintf\n')
[fopen_fileID, fopen_msg] = fopen('testfile', 'r');
while feof(fopen_fileID) == false
	fgets_return = fgets(fopen_fileID);
	printf('%s\n', strtrim(fgets_return)) 
end
fclose(fopen_fileID)
Write file contents using fprintf
Read back and display contents written using fprintf
print string                          **test**
print string   minimum size           **      test**
print string   string left justified  **test      **
print number                          **1234568**
print number   minimum size           **   1234568**
print number   left justified         **1234568   **
print number   zero fill              **0001234568**
print float                           **123.141593**
print float    minimum width          **    123.141593**
print float    left justified         **123.141593    **
print float    zero filled            **0000123.141593**
print float    3 decimal restriction  **0000000123.142**
print float    8 decimal expansion    **00123.14159265**
ans = 0
Write backslash
  cmd = sprintf('start /wait C:\\\\dummy.exe');
  fid = fopen('c:\cmdout.txt','w');
  fprintf(fid, cmd);
  fclose(fid);
  % Contents of c:\cmdout.txt will be "start /wait C:\dummy.exe"