Perform Data Read and Write Operations in Generated Code - MATLAB & Simulink (original) (raw)

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.

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 andsave as extrinsic functions, meaning that it does not generate C/C++ code for the function bodies. The generated code instead dispatches calls toload and save to the MATLAB engine for execution.

These additional restrictions apply when using load andsave in code generation:

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:

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:

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 andcoder.write is not supported.

Code generated for coder.read has two distinct advantages over code generated for the coder.load function:

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 withfscanf. 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

Topics