fwrite
Writes binary data to a file.
Category: Data Import and Analysis > Data Import and Export
Syntax
count = fwrite(fid, A)
Writes the elements of A as unsigned bytes.
count = fwrite(fid, A, precision)
Writes in the given type, such as 'int16' or 'double'.
count = fwrite(fid, A, precision, skip, machinefmt)
Skips bytes between values and sets the byte order.
Description
Values are converted to the requested type before writing, saturating at its limits. The result is a raw byte stream with no header, so whoever reads it must know the type and byte order. Pair it with fread using the same precision.
Input arguments
A- Numeric or character data, written in column order.
precision- Output type:
'uint8'(default),'int8','uint16','int16','uint32','int32','single','double','char'. machinefmt'l'for little-endian or'b'for big-endian.
Output arguments
count- Number of elements written.
Example
Bytes and 16-bit values
fid = fopen('b.bin', 'w');
n1 = fwrite(fid, [1 2 3])
n2 = fwrite(fid, [1 300], 'uint16')
fclose(fid);
d = dir('b.bin'); d.bytes
fid = fopen('b.bin');
fread(fid, 3)'
fread(fid, 2, 'uint16')'
fclose(fid);
Output
n1 = 3
n2 = 2
ans = 7
ans = 1×3
1 2 3
ans = 1×2
1 300