Convert MATLAB Coder Project to MATLAB Script

After you define input types, you can convert a MATLAB® Coder™ project to the equivalent script of MATLAB commands. The script reproduces the project in a configuration object and runs the codegen command. You can:

  • Move from a project workflow to a command-line workflow.

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

You can convert a project using the MATLAB Coder app or the command-line interface.

Project to script conversion does not support entry-point function inputs that are value classes.

Project to script conversion is not supported in MATLAB Online™.

Convert a Project Using the MATLAB Coder App

  1. On the app toolbar, click , and then select Convert to script.

  2. Specify the script name and click Save.

Convert a Project Using the Command-Line Interface

To convert a project to a script using the command-line interface, use the -tocode option of the coder command. The project file must be on the search path.

For example, to convert the project, myproject.prj to the script named myscript.m use this command:

coder -tocode myproject -script myscript.m

The coder command overwrites a file that has the same name as the script. If you omit the -script option, the coder command writes the script to the Command Window.

For more information about the -tocode option, see coder.

Run the Script

  1. Make sure that the entry-point functions that are arguments to codegen in the script are on the search path.

  2. Run the script. For example:


The following variables appear in the base workspace.

cfgConfiguration object
ARGSTypes of input arguments, if the project has entry-point function inputs
ARGTypes of cell array elements, if the project has cell array inputs. A script can reuse ARG for different cell array elements
GLOBALSTypes and initial values of global variables, if the project has global variables

cfg, ARGS, ARG, and GLOBALS appear in the workspace only after you run the script. The type of configuration object depends on the project file settings.

Project File Settings in MATLAB Coder AppCode Configuration Object

Build type is MEX.


Build type is static library, dynamically linked library, or executable.

One of the following conditions is true:

  • You do not have an Embedded Coder® license.

  • You have an Embedded Coder license. On the All Settings tab, Use Embedded Coder features is set to No.


Build type is static library, dynamically linked library, or executable.

You have an Embedded Coder license. On the All Settings tab, Use Embedded Coder features is set to Yes.


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

For a project that includes fixed-point conversion, project to script conversion generates a pair of scripts for fixed-point conversion and fixed-point code generation. For an example, see Convert Fixed-Point Conversion Project to MATLAB Scripts.

Special Cases That Generate Additional MAT-File

Suppose that you convert a project file myproject.prj to a script myscript.m. In certain situations the code generator can produce an additional MAT-file in the current working folder. 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.prj 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 coder.Constant objects to define constant inputs or constant global variables.

  • One or more of these coder.Constant objects are created from values that are structures, cell arrays, value classes, or large numeric constants (greater than a certain threshold). 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, modify the project file myproject.prj:

  • Open myproject.prj in the MATLAB Coder app.

  • Navigate to the Define Input Types page.

  • Enter the constant values of the inputs or the global variables directly in the app. This action automatically saves the modified myproject.prj.

See Also


Related Topics