MATLAB Function Reference Previous page   Next Page

Write binary data to file



count = fwrite(fid, A, precision) writes the elements of matrix A to the specified file, translating MATLAB values to the specified precision. The data is written to the file in column order, and a count is kept of the number of elements written successfully. errmsg is an optional output argument that returns an error message string if an error occurred, or an empty string if an error did not occur.

fid is an integer file identifier obtained from fopen, or 1 for standard output or 2 for standard error.

precision controls the form and size of the result. See fread for a list of allowed precisions. For 'bitN' or 'ubitN' precisions, fwrite sets all bits in A when the value is out of range.

count = fwrite(fid, A, precision, skip) includes an optional skip argument that specifies the number of bytes to skip before each precision value is written. With the skip argument present, fwrite skips and writes one value, skips and writes another value, etc., until all of A is written. If precision is a bit format like 'bitN' or 'ubitN', skip is specified in bits. This is useful for inserting data into noncontiguous fields in fixed-length records.


You cannot view or type the contents of the file you are writing with fwrite until you close the file with the fclose function.


For example,

creates a 100-byte binary file containing the 25 elements of the 5-by-5 magic square, stored as 4-byte integers.

See Also

fclose, ferror, fopen, fprintf, fread, fscanf, fseek, ftell

Previous page  funm fzero Next page

© 1994-2005 The MathWorks, Inc.