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.

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:

     myscript

The following variables appear in the base workspace.

VariableFor
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 output type.

Project Output TypeConfiguration Object
MEX Functioncoder.MexCodeConfig
C/C++ Static LibraryWithout an Embedded Coder® license: coder.CodeConfig
With an Embedded Coder license: coder.EmbeddedCodeConfig
C/C++ Dynamic Library
C/C++ Executable

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.

Was this topic helpful?