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
count = fwrite(___)
returns the number of elements ofA
thatfwrite
successfully writes to the file. You can use this syntax with any of the input arguments of the previous syntaxes.
Examples
Write uint8 Data to Binary File
Open a file namednine.bin
for writing. Specify write access using'w'
in the call tofopen
.
fileID = fopen('nine.bin','w');
fopen
returns 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);
Write 4-byte Integers to Binary File
Open a file namedmagic5.bin
for writing.
fileID = fopen('magic5.bin','w');
Write the 25 elements of the 5-by-5 magic square. Use theprecision
argument,'integer*4'
, to write 4-byte integers.
fwrite(fileID,magic(5),'integer*4');
Close the file.
fclose(fileID);
Append Data to Binary File
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);
与大端法写二进制文件Byte Ordering
Write random double-precision numbers to a file namedmyfile.bin
for use on a big-endian system. Specify amachinefmt
value 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
fileID
—File identifier
integer|1|2
File identifier, specified as an integer obtained fromfopen
,1
for standard output (the screen), or2
for standard error.
A
—Data to write
numeric array|character array|string array
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
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Integers, signed |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Floating-point numbers |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Characters |
|
|
|
Depends on the encoding scheme associated with the file. Set encoding with |
If you specify a precision ofbit
orn
ubit
, thenn
fwrite
saturates for all values outside the range.
Note
To preserveNaN
andInf
values in MATLAB®, read and write data of classdouble
orsingle
.
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 aprecision
ofbit
orn
ubit
, specifyn
skip
in bits.
Use theskip
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. Forbit
andn
ubit
precisions,n
machinefmt
specifies the order for writing bits within a byte, but the order for writing bytes remains your system byte ordering.
|
Your system byte ordering (default) |
|
Big-endian ordering |
|
Little-endian ordering |
|
Big-endian ordering, 64-bit long data type |
|
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'
,'bit
, andn
''ubit
.n
'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®.
See Also
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)