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 thecodegen
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 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™.
On the app toolbar, click, and then selectConvert to script.
Specify the script name and clickSave.
To convert a project to a script using the command-line interface, use the-tocode
option of thecoder
command. The project file must be on the search path.
For example, to convert the project,myproject.prj
to the script namedmyscript.m
use this command:
coder-tocodemyproject-scriptmyscript.m
Thecoder
命令覆盖file that has the same name as the script. If you omit the-script
option, thecoder
command writes the script to the Command Window.
For more information about the-tocode
option, seecoder
.
Make sure that the entry-point functions that are arguments tocodegen
in the script are on the search path.
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 reuseARG for 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:
|
编码器。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 to |
编码器。EmbeddedCodeConfig |
You can import the settings from the configuration objectcfg
into 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.
Suppose that you convert a project filemyproject.prj
to 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.prj
was generated by converting acodegen
command to an equivalentMATLAB Coderproject. SeeConvert codegen Command to Equivalent MATLAB Coder Project.
The originalcodegen
command uses编码器。Constant
objects to define constant inputs or constant global variables.
One or more of these编码器。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 filemyproject.prj
:
Openmyproject.prj
in 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
.