Documentation Center

  • Trial Software
  • Product Updates
我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

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

File identifier, specified as one of the following:

  • A 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

32 (4)

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*4

32 (4)

integer*8

64 (8)

schar

8 (1)

signed char

8 (1)

short

16 (2)

long

32 (4)

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.

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.

'n' or 'native'

Your system byte ordering (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-endian 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

| | | | | | |

Was this topic helpful?