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
loadmust be a constant character vector or string scalar. By contrast,savecan be passed a file name that is determined at run time.All other arguments passed to
loadandsavemust 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 Code Generation Time
To load data from MAT or ASCII files at the time of code generation, use the coder.load function. Because this function loads data during code
generation, these restrictions apply:
The file name that you pass to
coder.loadmust be a constant at code generation time.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 code generation
time. Therefore, these considerations apply:
The
coder.readfunction can read data only from a specific type of file that has the.coderdataextension. 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
.coderdatafiles, use thecoder.writefunction in MATLAB execution. Generating code for thecoder.writefunction is not supported.At run time, the
coder.readfunction can read data from any.coderdatafile whose data is consistent with the type and size you specified at code generation 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
.coderdatafiles 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