MATLAB Function Reference
fwrite

Write binary data to a file

Syntax

• ```count = fwrite(fid,A,`precision`)
count = fwrite(fid,A,`precision`,skip)
```

Description

```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.

`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.

Examples

For example,

• ```fid = fopen('magic5.bin','wb');
fwrite(fid,magic(5),'integer`*`4')
```

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

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