Write binary data to a file
Syntax
Description
count = fwrite(fid,A,precision)
writes the elements of matrix A
to the specified file, translating MATLAB values to the specified precision
. The data is written to the file in column order, and a count
is kept of the number of elements written successfully.
fid
is an integer file identifier obtained from fopen
, or 1
for standard output or 2
for standard error.
precision
controls the form and size of the result. For 'bitN'
or 'ubitN'
precisions, fwrite
sets all bits in A
when the value is out-of-range.
count = fwrite(fid,A,
includes an optional precision
,skip) skip
argument that specifies the number of bytes to skip before each precision
value is written. With the skip
argument present, fwrite
skips and writes one value, skips and writes another value, etc. until all of A
is written. If precision
is a bit format like 'bitN'
or 'ubitN'
, skip
is specified in bits. This is useful for inserting data into noncontiguous fields in fixed-length records.
Examples
creates a 100-byte binary file, containing the 25 elements of the 5-by-5 magic square, stored as 4-byte integers.
Write binary data to the device
Syntax
Arguments
Description
fwrite(obj,A)
writes the binary data A
to the device connected to obj
.
fwrite(obj,A,'
writes binary data with precision specified by precision
') precision
.
precision
controls the number of bits written for each value and the interpretation of those bits as integer, floating-point, or character values. If precision
is not specified, uchar
(an 8-bit unsigned character) is used.
writes binary data with command line access specified by fwrite(obj,A,'
mode
')
mode
. If mode
is sync
, A
is written synchronously and the command line is blocked. If mode
is async
, A
is written asynchronously and the command line is not blocked. If mode
is not specified, the write operation is synchronous.
writes binary data with precision specified by fwrite(obj,A,'
precision
','
mode
')
precision
and command line access specified by mode
.
Remarks
Before you can write data to the device, it must be connected to obj
with the fopen
function. A connected serial port object has a Status
property value of open
. An error is returned if you attempt to perform a write operation while obj
is not connected to the device.
The ValuesSent
property value is increased by the number of values written each time fwrite
is issued.
An error occurs if the output buffer cannot hold all the data to be written. You can specify the size of the output buffer with the OutputBufferSize
property.
If you use the help
command to display help for fwrite
, then you need to supply the pathname shown below.
Synchronous Versus Asynchronous Write Operations
By default, data is written to the device synchronously and the command line is blocked until the operation completes. You can perform an asynchronous write by configuring the mode
input argument to be async
. For asynchronous writes:
- The
BytesToOutput
property value is continuously updated to reflect the number of bytes in the output buffer. - The M-file callback function specified for the
OutputEmptyFcn
property is executed when the output buffer is empty.
You can determine whether an asynchronous write operation is in progress with the TransferStatus
property.
Synchronous and asynchronous write operations are discussed in more detail in Writing Data.
Rules for Completing a Write Operation with fwrite
A binary write operation using fwrite
completes when:
- The specified data is written.
- The time specified by the
Timeout
property passes.
Note The Terminator property is not used with binary write operations. |
Supported Precisions
precision
are listed below.
No comments:
Post a Comment