Main Content

Convert codegen Command to Equivalent MATLAB Coder Project

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 arguments input_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 incomplete codegen 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. Specify myadd as the entry-point function. Specify the inputs to myadd as variable-size matrices of type double whose dimensions are unbounded. Specify cfg as the code configuration object. Include the -toproject option to convert the codegen 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 another codegen 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. Specify cfg 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) object obj.

    Certain fi object properties are enabled by other properties. When you construct a fi object, these properties are set to their default values unless you explicitly modify them. In obj, you set one or more properties that are not enabled to non-default values. See Set fi Object Properties (Fixed-Point Designer).

    You convert this codegen command to a project file by using the -toproject option. You build the project file and generate a MEX function. When you pass obj 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 of obj that are not enabled to their default values before passing it to the MEX function. To do this, define a new fi object obj_new:

    a = mat2str(obj);
    obj_new = eval(a);

    Pass obj_new as the constant input to the generated MEX function.

See Also

Related Topics