Perform Data Read and Write Operations in Generated Code
The code generator provides you with multiple alternative ways of reading and writing data from files, each of which has its own use cases, advantages, and limitations. For example, depending on your code generation target, if data files change at run time, or if you need to perform low-level file I/O operations, certain approaches can be better suited for your application than others.
In MATLAB® execution, you typically use the load
and save
functions to perform high-level data
read and write operations. Because the MATLAB engine is available during MEX execution or Simulink® simulation, you can use load
and save
for these code generation targets.
However, for standalone code generation, these functions are not supported and you must use
functions specific to code generation such as coder.load
and coder.read
.
Alternatively, if you want to perform custom low-level custom file I/O operations, it is
more convenient to use functions such as fscanf
and fprintf
in both MATLAB execution and code generation.
Read this topic to decide which data I/O approach is suited for your application.
Perform File I/O in MEX or Simulink Simulation
For simulation targets, to perform data I/O with MAT files, use the load
and save
functions. These functions are not
supported for standalone code generation.
The code generator automatically treats load
and
save
as extrinsic functions, meaning that it does not generate
C/C++ code for the function bodies. The generated code instead dispatches calls to
load
and save
to the MATLAB engine for execution.
These additional restrictions apply when using load
and
save
in code generation:
The file name passed to
load
must be a constant character vector or string scalar. By contrast,save
can be passed a file name that is determined at run time.All other arguments passed to
load
andsave
must be string scalar or character vector literals.
For additional restrictions, see the Extended Capabilities
sections in the load
and save
function reference
pages.
Load Data at Compile Time
To load data from MAT or ASCII files at compile time, use the coder.load
function. Because this function loads data during code
generation, these restrictions apply:
The file name that you pass to
coder.load
must be a compile-time constant.If you change the contents of the loaded file after you generate code, the change is not reflected in the behavior of the generated code.
Load Data at Run Time
To load data into the generated code at run time, use the coder.read
function in your MATLAB code for which you want to generate C/C++ code. For the generated code to
correctly interpret the content of the data file, the
type and size of the data must be available to the code generator at compile time.
Therefore, these considerations apply:
The
coder.read
function can read data only from a specific type of file that has the.coderdata
extension. These files include a type header that contains information about the type and size of the data in the file.To store your workspace variables in
.coderdata
files, use thecoder.write
function in MATLAB execution. Generating code for thecoder.write
function is not supported.At run time, the
coder.read
function can read data from any.coderdata
file whose data is consistent with the type and size you specified at compile time.
In contrast with MAT-files that can be read only inside the MATLAB environment, you can read .coderdata
files on
any deployment platform that supports a file system.
In addition, the .coderdata
format supports most primitive and
aggregate MATLAB data types, including arrays, structures, and cell arrays. So, the C/C++
code generated for coder.read
can be used to read complex aggregate
data from .coderdata
files into your deployed application. However,
reading and writing of objects using coder.read
and
coder.write
is not supported.
Code generated for coder.read
has two distinct advantages over
code generated for the coder.load
function:
You can update the data stored in
.coderdata
files without having to regenerate code, as long as the type and size of the new data matches those of the old data.The data is not hard-coded in the generated code, thereby improving the readability of the generated code.
For an example, see Read Data Whose Size Can Change at Run Time.
Perform Low-Level File I/O Operations
The MATLAB functions fscanf
, fprintf
, fread
, and fwrite
perform low-level I/O operations with text and binary files. In
particular, comma-separated value (CSV) formatted files are well-suited for use with
fscanf
. They are supported for code generation with certain
restrictions, as described in the Extended Capabilities section of
the documentation for each function. By using these functions in your MATLAB code, you can generate code that performs highly customized read and write
operations on text and binary files at run time.
For examples, see:
See Also
load
| save
| coder.load
| coder.read
| coder.write
| fread
| fwrite
| fscanf
| fprintf