Main Content

Data Read and Write Considerations

To generate code for MATLAB® code that reads data files, you must identify your code generation target, file-type, and the supported read and write function. The following table presents the read and write functions and their usage notes:

MATLAB FunctionFile TypeHow to Use

coder.write and coder.read

.coderdata file

Usage notes:

  • Use coder.write in MATLAB execution to store your workspace variables in .coderdata files.

  • Use coder.read in MATLAB code for which you want to generate C/C++ code.

    You must specify the type and size of the data you want to read at code generation time, also referred to as compile time.

    This function can read data from any .coderdata file at run time whose data is consistent with the type and size you specified at compile time.

Example: Read Data from Multiple .coderdata Files at Run Time

coder.load

MAT-file or ASCII file

Usage notes:

  • coder.load(filename) loads data at code generation time, also referred to as compile time.

  • filename must be a compile-time constant. If you change the content of filename after you generate code, the change is not reflected in the behavior of the generated code.

load and save

MAT-file or ASCII file

Usage notes:

  • Use these functions only when generating MEX functions or code for Simulink® simulation.

  • The code generator does not produce code for the load or save call body, instead dispatching the call to the MATLAB engine for execution.

  • save can be passed a filename that is determined at run time. The filename passed to load must be a constant character vector or string scalar.

  • All other arguments passed to load and save must be string scalar or character vector literals.

  • Text file

  • Binary file

Usage notes:

  • These functions perform low level I/O operations with text and binary files. They are supported for code generation with certain restrictions, as described in the Extended Capabilities sections of the function reference pages.

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

Examples:

See Also

| | | | | |

Related Topics