Main Content

coder.write

Create data files that the generated code reads at run time

Since R2023a

    Description

    example

    coder.write(fileName,data) stores the argument data in a file with the name fileName.coderdata in your current directory. Use this function in MATLAB® execution only and not the MATLAB code you intend to use for C/C++ code generation.

    Use the coder.read function to read data from a .coderdata file in MATLAB and in the generated code.

    A .coderdata file contains a type header that specifies the type and size of the data stored in the file. The code generated for the coder.read function can read any .coderdata file at run-time, whose type and size is consistent with the type and size information that you supply to coder.read during code generation.

    coder.write(fileName,data,Name=Value) creates 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. Use the name-value arguments 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.

    Examples

    collapse all

    This example shows how to 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 double type in your workspace.

    c = rand(20);

    Store this variable in a file named storageFile.coderdata in your current directory.

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

    This example shows how to create a storage file for multiple variables.

    Create two arrays of double type in your workspace. Array a is of size 10-by-10 and array b is 20-by-20.

    a = rand(10);
    b = rand(20);

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

    s = struct('a',a,'b',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.

    In this example, you generate code for a coder.read command that can read multiple .coderdata files at run time. These files contain array data that have the same type, but different sizes. To generate code, you must pass a type header file that is consistent with all your individual data files to the coder.read function call.

    Create the storage file file_a.coderdata, which contains two variables with array data of type double. Variables a and b are of different sizes.

    a = rand(10,20);
    b = rand(5,30);
    coder.write("file_a.coderdata",a);

    A coder.Type object that is consistent with both variables a and 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.

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

    You can modify the header information of file_a.coderdata and use the modified file as the source of the type header information.

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

    You can also create the file_b.coderdata file with the required type header information by running this command.

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

    Alternatively, you can 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 any actual data.

    coder.write("file_b.coderdata",b,TypeHeader=t,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.

    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 specify whether to omit the data and create a .coderdata file with the type header only, 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 specify whether to report when writing a.coderdata file at the MATLAB command line, specified as true or false.

    Version History

    Introduced in R2023a