Main Content

coder.write

Create data files that the generated code reads at run time

Since R2023a

    Description

    Use the coder.write function to create data files that the generated code can read at run time. These files have the .coderdata extension. Use this function in MATLAB® execution only and not the MATLAB code you intend to use for C/C++ code generation.

    To read data from a .coderdata file in MATLAB execution and in the generated code, use the coder.read function. A .coderdata file contains a type header that specifies the type and size of the data stored in the file. The coder.read function uses this information when interpreting the contents of the file.

    coder.write(filename,data) stores the variable data in a file with the name filename.coderdata in the current folder.

    example

    coder.write(filename,data,Name=Value) accepts additional name-value arguments that you can use to:

    • Specify a custom type header for the .coderdata file that is consistent with the variable data.

    • Omit the actual data and create a .coderdata file with a type header only.

    Use this syntax to create a type header file, which is a .coderdata file that you use to specify the type and size of data to the coder.read function.

    example

    Examples

    collapse all

    Create a storage file for a single variable. This storage file can be read at run-time by using the coder.read function.

    Create a 20-by-20 array of type double in your workspace.

    c = rand(20);

    Store this variable in a file named storageFile.coderdata in the current folder.

    coder.write("storageFile.coderdata",c);

    Create a storage file for multiple variables.

    Create two arrays of type double in your workspace. The sizes of arrays var_a and var_b are 10-by-10 and 20-by-20, respectively.

    var_a = rand(10);
    var_b = rand(20);

    Create a structure s that contains arrays var_a and var_b in its fields.

    s = struct('a',var_a,'b',var_b);

    Store the structure s in a .coderdata file.

    coder.write("storageFile.coderdata",s);

    Alternatively, you can use a cell array to store multiple variables in a .coderdata file.

    Suppose that you want to generate code that can read multiple .coderdata files at run time. These files contain array data that have the same type, but different sizes. To achieve this goal, you must pass a type header file that is consistent with all your individual data files to the coder.read function call in your entry-point function. This example shows how to create a such a type header file. For the actual code generation workflow, see Read Data Whose Size Can Change at Run Time.

    At the command line, create variables var_a and var_b that are both of type double but have different sizes. Use the coder.write function to store these arrays in the files file_a.coderdata and file_b.coderdata, respectively.

    var_a = rand(10,20);
    var_b = rand(5,30);
    coder.write("file_a.coderdata",var_a)
    coder.write("file_b.coderdata",var_b)

    A coder.Type object that is consistent with both variables var_a and var_b must have variable-size dimensions. The upper bounds of the two array dimensions must be at least 10 and 30, respectively. Create a coder.Type object that represents a variable-size double type with these bounds.

    common_type = coder.typeof(var_a,[10 30],[1 1])
    common_type = 
    
    coder.PrimitiveType
       :10×:30 double

    Modify the header information of file_a.coderdata to be compatible with both arrays var_a and var_b. You can use the modified file as your desired common type header file.

    coder.write("file_a.coderdata",a,TypeHeader=common_type);

    Alternatively, you can also modify the file_b.coderdata file with the required type header information by running this command.

    coder.write("file_b.coderdata",b,TypeHeader=common_type);

    Another alternative is to create a separate type header file myTypeHeader.coderdata that contains only the type header compatible with all the existing storage files and does not contain actual data.

    coder.write("file_b.coderdata",var_b,TypeHeader=common_type,TypeHeaderOnly=true);

    Such a type header file is useful if you are working with large data files and want to use the generated file as a type header file only.

    Input Arguments

    collapse all

    Name or full file path of a new or existing storage file, specified as a string scalar or character vector. The generated storage file has the name filename.coderdata, or just filename if the value you supplied already has the extension.

    Data source to save in the storage file, specified as an array or multiple arrays stored within a structure or cell array.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | cell | categorical | sparse
    Complex Number Support: Yes

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    Example: coder.write('example.coderdata',rand(1000),TypeHeader=coder.typeof(1,[1000 1000],1 1]),TypeHeaderOnly=true)

    Type information for storage data, specified as a coder.Type object. This type object must be compatible with the input argument data. The default value of this object is coder.typeof(data).

    To create a type object, use the coder.typeof or coder.newtype function. You can also create and edit coder.Type objects interactively by using the Coder Type Editor. See Create and Edit Input Types by Using the Coder Type Editor.

    Example: coder.write("example.coderdata",rand(10),TypeHeader=coder.typeof(1,[10 10],1 1]))

    Option to create a .coderdata file with the type header only and omit the data, specified as true or false. If data is a large array and you want to use the .coderdata file as a type header file only, set this argument to true.

    Example: coder.write("example.coderdata",rand(1000),TypeHeaderOnly=true)

    Option to display a status message when writing a .coderdata file at the MATLAB command line, specified as true or false.

    Version History

    Introduced in R2023a