Convert codegen Command to Equivalent MATLAB Coder Project - MATLAB & Simulink (original) (raw)
Main Content
You can use the codegen command with the-toproject
option to convert a codegen
command to an equivalent MATLAB® Coder™ project file. You can then generate code from the project file by using another codegen
command or the MATLAB Coder app.
For example, to convert a codegen
command with input argumentsinput_arguments
to the project file myProject.prj
, run:
codegen input_arguments -toproject myProject.prj
Input arguments to codegen
include:
- Names of entry-point functions
- Input type definitions specified by using the
-args
option - Code generation options, including parameters specified in configuration objects
- Names of custom source files to include in the generated code
You can also use the -toproject
option to convert an incompletecodegen
command to a project file. For example, to create a project file myProjectTemplate.prj
that contains only the code generation parameters stored in the configuration object cfg
, run:
codegen -config cfg -toproject myProjectTemplate.prj
myProjectTemplate.prj
does not contain specifications of entry-point functions or input types. So, you cannot generate code from this project file. You can open myProjectTemplate.prj
in the MATLAB Coder app and use it as a template to create full project files that you can use to generate code.
Note
Running the codegen
command with the-toproject
option does not generate code. It creates only the project file.
Example: Convert a Complete codegen
Command to a Project File
Define a MATLAB function, myadd
, that returns the sum of two values.
function y = myadd(u,v) %#codegen y = u + v; end
Create a coder.CodeConfig
object for generating a static library. Set TargetLang
to 'C++'
.
cfg = coder.config('lib'); cfg.TargetLang = 'C++';
At the MATLAB command line, create and run a codegen
command. Specifymyadd
as the entry-point function. Specify the inputs tomyadd
as variable-size matrices of type double
whose dimensions are unbounded. Specify cfg
as the code configuration object. Include the -toproject
option to convert thecodegen
command to an equivalent MATLAB Coder project file with name myadd_project.prj
.
codegen -config cfg myadd -args {coder.typeof(1,[Inf,Inf]),coder.typeof(1,[Inf,Inf])} -toproject myadd_project.prj
Project file 'myadd_project.prj' was successfully created. Open Project
The code generator creates the project file myadd_project.prj
in the current working folder. Running codegen
with the-toproject
option does not generate code. It creates only the project file.
Generate code from myadd_project.prj
by using anothercodegen
command.
codegen myadd_project.prj
The code generator produces a C++ static library function myadd
in the _`work`_\codegen\lib\myadd
folder, where_`work`_
is your current working directory.
Example: Convert an Incomplete codegen
Command to a Template Project File
Create a coder.CodeConfig
object for generating a static library. Set TargetLang
to 'C++'
.
cfg = coder.config('lib'); cfg.TargetLang = 'C++';
At the MATLAB command line, create and run a codegen
command. Specifycfg
as the code configuration object. Include the-toproject
option to convert the codegen
command to an equivalent MATLAB Coder project file with name myProjectTemplate.prj
.
codegen -config cfg -toproject myProjectTemplate.prj
Project file 'myProjectTemplate.prj' was successfully created. Open Project
You can now open myProjectTemplate.prj
in the MATLAB Coder app and use it as a template to create full project files that you can use to generate code.
Limitations
When you use the codegen
command with the-toproject
option, these limitations apply:
- Exporting the
CodeTemplate
parameter of a coder.EmbeddedCodeConfig object to a project file is not supported. - Suppose that your
codegen
command for generating a MEX function uses coder.Constant to define a constant input that is a fi (Fixed-Point Designer) objectobj
.
Certainfi
object properties are enabled by other properties. When you construct afi
object, these properties are set to their default values unless you explicitly modify them. Inobj
, you set one or more properties that are not enabled to non-default values. See Set fi Object Properties (Fixed-Point Designer).
You convert thiscodegen
command to a project file by using the-toproject
option. You build the project file and generate a MEX function. When you passobj
as the constant input argument to the generated MEX function and run the MEX, the MEX might throw an error.
To fix this issue, you must set the properties ofobj
that are not enabled to their default values before passing it to the MEX function. To do this, define a newfi
objectobj_new
:
a = mat2str(obj);
obj_new = eval(a);
Passobj_new
as the constant input to the generated MEX function.