Skip to Main Content Skip to Search
Product Documentation

fwrite - Write data to binary file

Syntax

fwrite(fileID, A)
fwrite(fileID, A, precision)
fwrite(fileID, A, precision, skip)
fwrite(fileID, A, precision, skip, machineformat)
count = fwrite(...)

Description

fwrite(fileID, A) writes the elements of array A to a binary file in column order.

fwrite(fileID, A, precision) translates the values of A according to the form and size described by the precision.

fwrite(fileID, A, precision, skip) skips skip bytes before writing each value. If precision is bitn or ubitn, specify skip in bits.

fwrite(fileID, A, precision, skip, machineformat) writes data with the specified machineformat. The skip parameter is optional.

count = fwrite(...) returns the number of elements of A that fwrite successfully writes to the file.

Input Arguments

fileID

One of the following:

  • An integer file identifier obtained from fopen.

  • 1 for standard output (the screen).

  • 2 for standard error.

A

Numeric or character array.

precision

String in single quotation marks that controls the form and size of the output. The following table shows possible values for precision.

Value TypePrecisionBits (Bytes)

Integers, unsigned

uint

32 (4)

uint8

8 (1)

uint16

16 (2)

uint32

32 (4)

uint64

64 (8)

uchar

8 (1)

unsigned char

8 (1)

ushort

16 (2)

ulong

system-dependent

ubitn

1n64

Integers, signed

int

32 (4)

int8

8 (1)

int16

16 (2)

int32

32 (4)

int64

64 (8)

integer*1

8 (1)

integer*2

16 (2)

integer*3

32 (4)

integer*4

64 (8)

schar

8 (1)

signed char

8 (1)

short

16 (2)

long

system-dependent

bitn

1n64

Floating-point numbers

single

32 (4)

double

64 (8)

float

32 (4)

float32

32 (4)

float64

64 (8)

real*4

32 (4)

real*8

64 (8)

Characters

char*1

8 (1)

char

Depends on the encoding scheme associated with the file. Set encoding with fopen.

On 32-bit systems, long and ulong are 32 bits. On 64-bit systems, the sizes of long and ulong correspond to the data model that the C compiler uses: 32 bits on Microsoft Windows systems, 64 bits on other 64-bit systems.

If you specify a precision of bitn or ubitn, and a value is out of range, fwrite sets all bits for that value.

Default: uint8

skip

Number of bytes to skip before writing each value. If you specify a precision of bitn or ubitn, specify skip in bits. Use this parameter to insert data into noncontiguous fields in fixed-length records.

Default: 0

machineformat

String that specifies the order for writing bytes within the file. For bitn and ubitn precisions, specifies the order for writing bits within a byte.

Possible values are:

'n' or 'native'

The byte ordering that your system uses (default)

'b' or 'ieee-be'

Big-endian ordering

'l' or 'ieee-le'

Little-endian ordering

's' or 'ieee-be.l64'

Big-endian ordering, 64-bit long data type

'a' or 'ieee-le.l64'

Little-endian ordering, 64-bit long data type

By default, all currently supported platforms use little-endian ordering for new files. Existing binary files can use either big- or little-endian ordering.

Examples

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

fid = fopen('magic5.bin', 'w');
fwrite(fid, magic(5), 'integer*4');
fclose(fid);

See Also

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

How To

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS