Main Content

Convert MATLAB Coder Project to MATLAB Script

You can convert a MATLAB® Coder™ project to an equivalent script of MATLAB commands. The created script uses the definitions in the MATLAB Coder project to create a configuration object, define input types, and construct a codegen command. By converting a MATLAB Coder project to a script, you can:

  • Replicate app settings and commands at the command line.

  • Save the project as a text file that you can share.

Converting Project to Script

To convert a project to a script, use one of these approaches:

  • On the MATLAB Coder app toolstrip, click the Export button and select Convert To Script.

  • At the command line, use the -tocode option with the coder command. If the project file is not in the working directory, you must specify the file path.

Running the Script

Before you run the created script, make sure that the entry-point functions used by the script are on the search path. Then, call the script at the command line.

This table shows the variables that the script creates in the base workspace.

Variable NameDescription
cfgConfiguration object.
inputTypesCell array that contains the input arguments for each entry point, if the project has entry-point function inputs.
childTypesStructure that contains the types of cell array elements, structure fields, and class properties, if the project has these input types. A script can reuse childTpes for different aggregate inputs.
globalVariablesCell array that contains the types and initial values of global variables, if the project has global variables.

The type of configuration object that the script creates depends on the build type of your MATLAB Coder project and on the configuration parameter Use Embedded Coder features.

Build TypeParameter SettingCreated Configuration Object
MEXAnycoder.MexCodeConfig
Standalone (static library, dynamic library, or executable) Use Embedded Coder features check box selectedcoder.EmbeddedCodeConfig
Standalone (static library, dynamic library, or executable) Use Embedded Coder features check box clearedcoder.CodeConfig

You can import the settings from the configuration object cfg into a project. See Share Build Configuration Settings.

Special Cases That Generate Additional MAT File

Suppose that you convert a project file myproject.coderprj to a script myscript.m. In certain situations, the code generator can produce an additional MAT file in the same folder as the exported script. In such cases, the generated script loads the MAT file and uses the stored values to define constant inputs or constant global variables in the generated code.

This behavior happens if all of these conditions are true:

  • The project file myproject.coderprj was generated by converting a codegen command to an equivalent MATLAB Coder project. See Convert codegen Command to Equivalent MATLAB Coder Project.

  • The original codegen command uses global variables or uses coder.Constant objects to define constant inputs.

  • The code generator is unable to create a script that recreates the variables in the coder project file because doing so would be impractical or impossible. In such cases, the generated MAT file stores these values.

Even if all of the preceding conditions are true, you can avoid the creation of the auxiliary MAT file. Before generating the script, open the project file in the MATLAB Coder app. On the Entry Points pane, specify the constant values of the input arguments or global variables. Then, generate the script.

See Also

|

Topics