fwrite(fileID,A) writes the
elements of array A as 8-bit unsigned integers to a binary file in
column order. The binary file is indicated by the file identifier,
fileID. Use fopen to open the file and obtain
the fileID value. When you finish writing, close the file by calling
fclose(fileID).
fwrite(fileID,A,precision,skip,machinefmt) additionally
specifies the order for writing bytes or bits to the file. The skip argument
is optional.
count = fwrite(___) returns
the number of elements of A that fwrite successfully
writes to the file. You can use this syntax with any of the input
arguments of the previous syntaxes.
Write random double-precision numbers to a file named myfile.bin for use on a big-endian system. Specify a machinefmt value of 'ieee-be' in the call to fwrite, to indicate big-endian byte ordering.
File identifier, specified as an integer obtained from fopen, 1 for
standard output (the screen), or 2 for standard
error.
A — Data to write numeric array | character array | string array
Data to write, specified as a numeric, character, or string
array.
While fwrite supports writing character or string data, doing so
can result in unexpected behavior and is therefore not recommended.
If you use fwrite to write character or string data, specify the
text encoding when calling fopen to open the file for reading or
writing and specify the precision as char.
precision — Class and size of values to write 'uint8' (default) | character vector | string scalar
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'
1 ≤ n ≤ 64
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'
1 ≤ n ≤ 64
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'
The MATLAB®char type is not a fixed size, and the
number of bytes depends on the encoding scheme associated with the file.
Set encoding with fopen.
If you specify a precision of bitn or ubitn,
then fwrite saturates for all values outside the
range.
Note
To preserve NaN and Inf values
in MATLAB, read and write data of class double or single.
skip — Number of bytes to skip 0 (default) | scalar
Number of bytes to skip before writing each value, specified
as a scalar. If you specify a precision of bitn or ubitn,
specify skip in bits.
Use the skip argument to insert data into
noncontiguous fields in fixed-length records.
machinefmt — Order for writing bytes 'n' (default) | 'b' | 'l' | 's' | 'a'
Order for writing bytes within the file, specified as one of
the character vectors or string scalars in the table that follows.
For bitn and ubitn precisions, machinefmt specifies
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
C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The precision argument must be a constant.
Does not support precision types 'long', 'ulong',
'unsigned long', 'bitn',
and 'ubitn'.
Does not support the machine format (order for writing
bytes) input argument.
If the precision is a C type such as int,
the target and production sizes for that type must:
Match.
Map directly to a MATLAB integer type.
Treats a char type as a signed
8-bit integer. Use values from 0 through 127 only.
When appending to a file and using a skip argument,
it must be possible for the C run-time fseek to
seek beyond the end of the file and initialize unwritten bytes to
0. This behavior matches the behavior of POSIX® and Windows®.