Main Content

ConvertMATLABCoderProject toMATLABScript

After you define input types, you can convert aMATLAB®Coder™project to the equivalent script of MATLAB commands. The script reproduces the project in a configuration object and runs thecodegencommand. 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 theMATLAB Coderapp 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 inMATLAB Online™.

Convert a Project Using theMATLABCoder应用程序

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

  2. Specify the script name and clickSave.

Convert a Project Using the Command-Line Interface

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

For example, to convert the project,myproject.prjto the script namedmyscript.muse this command:

coder-tocodemyproject-scriptmyscript.m

Thecoder命令覆盖file that has the same name as the script. If you omit the-scriptoption, thecodercommand writes the script to the Command Window.

For more information about the-tocodeoption, seecoder.

Run the Script

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

  2. Run the script. For example:

    myscript

The following variables appear in the base workspace.

Variable For
cfg Configuration object
ARGS Types of input arguments, if the project has entry-point function inputs
ARG Types of cell array elements, if the project has cell array inputs. A script can reuseARGfor different cell array elements
GLOBALS Types 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 inMATLAB Coder应用程序 Code Configuration Object

Build type is MEX.

编码器。MexCodeConfig

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 theAll Settingstab,Use Embedded Coder featuresis set toNo.

编码器。CodeConfig

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

You have an Embedded Coder license. On theAll Settingstab,Use Embedded Coder featuresis set toYes.

编码器。EmbeddedCodeConfig

You can import the settings from the configuration objectcfginto a project. SeeShare 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, seeConvert Fixed-Point Conversion Project to MATLAB Scripts.

Special Cases That Generate Additional MAT-File

Suppose that you convert a project filemyproject.prjto a scriptmyscript.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 filemyproject.prjwas generated by converting acodegencommand to an equivalentMATLAB Coderproject. SeeConvert codegen Command to Equivalent MATLAB Coder Project.

  • The originalcodegencommand uses编码器。Constantobjects to define constant inputs or constant global variables.

  • One or more of these编码器。Constantobjects 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 filemyproject.prj:

  • Openmyproject.prjin theMATLAB Coder应用程序。

  • Navigate to theDefine Input Typespage.

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

See Also

|

Related Topics