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,machinefmt)
count = fwrite(___)

Description

example

fwrite(fileID,A)write the elements of arrayAas 8-bit unsigned integers to a binary file in column order. The binary file is indicated by the file identifier,fileID. Usefopento open the file and obtain thefileIDvalue. When you finish reading, close the file by callingfclose(fileID).

example

fwrite(fileID,A,precision)writes the values inAin the form and size described byprecision.

fwrite(fileID,A,precision,skip)skips the number of bytes or bits specified byskipbefore writing each value.

example

fwrite(fileID,A,precision,skip,machinefmt)additionally specifies the order for writing bytes or bits to the file. Theskipargument is optional.

count = fwrite(___)returns the number of elements ofAthatfwritesuccessfully writes to the file. You can use this syntax with any of the input arguments of the previous syntaxes.

Examples

collapse all

Open a file namednine.binfor writing. Specify write access using'w'in the call tofopen.

fileID = fopen('nine.bin','w');

fopenreturns a file identifier,fileID.

Write the integers from 1 to 9 as 8-bit unsigned integers.

fwrite(fileID,[1:9]);

Close the file.

fclose(fileID);

Open a file namedmagic5.binfor writing.

fileID = fopen('magic5.bin','w');

Write the 25 elements of the 5-by-5 magic square. Use theprecisionargument,'integer*4', to write 4-byte integers.

fwrite(fileID,magic(5),'integer*4');

Close the file.

fclose(fileID);

Write a binary file containing the elements of the 4-by-4 magic square, stored as double-precision floating-point numbers.

fileID = fopen('magic4.bin','w'); fwrite(fileID,magic(4),'double'); fclose(fileID);

Open the file,magic4.bin, with write-access that enables appending to the file. Specify the file-access type,'a', in the call tofopen.

fileID = fopen('magic4.bin','a');

Append a 4-by-4 matrix of zeros to the file. Then, close the file.

fwrite(fileID,zeros(4),'double'); fclose(fileID);

Write random double-precision numbers to a file namedmyfile.binfor use on a big-endian system. Specify amachinefmtvalue of'ieee-be'in the call tofwrite, to indicate big-endian byte ordering.

fileID = fopen('myfile.bin','w'); fwrite(fileID,rand(4),'double','ieee-be'); fclose(fileID);

Input Arguments

collapse all

File identifier, specified as an integer obtained fromfopen,1for standard output (the screen), or2for standard error.

Data to write, specified as a numeric, character, or string array.

Example:[1,2,3;4,5,6]

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string
Complex Number Support:Yes

Class and size in bits of the values to write, specified as one of the character vectors or string scalars listed in the Precision column.

Value Type Precision Bits (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 withfopen.

If you specify a precision ofbitnorubitn, thenfwritesaturates for all values outside the range.

Note

To preserveNaNandInfvalues in MATLAB®, read and write data of classdoubleorsingle.

Number of bytes to skip before writing each value, specified as a scalar. If you specify aprecisionofbitnorubitn, specifyskipin bits.

Use theskipargument to insert data into noncontiguous fields in fixed-length records.

Order for writing bytes within the file, specified as one of the character vectors or string scalars in the table that follows. Forbitnandubitnprecisions,machinefmtspecifies the order for writing bits within a byte, but the order for writing bytes remains your system byte ordering.

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

Extended Capabilities

Introduced before R2006a

Was this topic helpful?