Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

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.

Inputs

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
uint8
uint16
uint32
uint64
uchar
unsigned char
ushort
ulong
ubitn

32 (4)
8 (1)
16 (2)
32 (4)
64 (8)
8 (1)
8 (1)
16 (2)
system-dependent
1n64

Integers, signed

int
int8
int16
int32
int64
integer*1
integer*2
integer*3
integer*4
schar
signed char
short
long
bitn

32 (4)
8 (1)
16 (2)
32 (4)
64 (8)
8 (1)
16 (2)
32 (4)
64 (8)
8 (1)
8 (1)
16 (2)
system-dependent
1n64

Floating-point numbers

single
double
float
float32
float64
real*4
real*8

32 (4)
64 (8)
32 (4)
32 (4)
64 (8)
32 (4)
64 (8)

Characters

char*1
char

8 (1)
Depends on the encoding scheme associated with the file. Set encoding with fopen.

long and ulong are 32 bits on 32-bit systems, and 64 bits on 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. Use this parameter when you plan to read and write to the same file on different systems.

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 data type

'a' or 'ieee-le.l64'

Little-endian ordering, 64-bit data type

Windows systems use little-endian ordering, and most UNIX systems use big-endian ordering, for both bytes and bits. Solaris systems use big-endian ordering for bytes, but little-endian ordering for bits.

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

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS