Main Content

ConvertcodegenCommand to EquivalentMATLABCoderProject

You can use thecodegencommand with the-toprojectoption to convert acodegencommand to an equivalentMATLAB®Coder™project file. You can then generate code from the project file by using anothercodegencommand or theMATLAB Coder应用程序。

For example, to convert acodegencommand with input argumentsinput_argumentsto the project filemyProject.prj, run:

codegeninput_arguments-toprojectmyProject.prj

Input arguments tocodegeninclude:

  • Names of entry-point functions

  • Input type definitions specified by using the-argsoption

  • 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选择将一个不完整的codegencommand to a project file. For example, to create a project filemyProjectTemplate.prjthat contains only the code generation parameters stored in the configuration objectcfg, run:

codegen-configcfg-toprojectmyProjectTemplate.prj
myProjectTemplate.prjdoes not contain specifications of entry-point functions or input types. So, you cannot generate code from this project file. You can openmyProjectTemplate.prjin theMATLAB Coderapp and use it as a template to create full project files that you can use to generate code.

Note

Running thecodegencommand with the-toprojectoption does not generate code. It creates only the project file.

Example: Convert a CompletecodegenCommand to a Project File

Define a MATLAB function,myadd, that returns the sum of two values.

functiony = myadd(u,v)%#codegeny = u + v;end

Create acoder.CodeConfigobject for generating a static library. SetTargetLangto'C++'.

cfg = coder.config('lib'); cfg.TargetLang ='C++';

At the MATLAB command line, create and run acodegencommand. Specifymyaddas the entry-point function. Specify the inputs tomyaddas variable-size matrices of typedoublewhose dimensions are unbounded. Specifycfgas the code configuration object. Include the-toprojectoption to convert thecodegencommand to an equivalentMATLAB Coderproject file with namemyadd_project.prj.

codegen-configcfgmyadd-args{coder.typeof(1,[Inf,Inf]),coder.typeof(1,[Inf,Inf])}-toprojectmyadd_project.prj
Project file 'myadd_project.prj' was successfully created.Open Project

The code generator creates the project filemyadd_project.prjin the current working folder. Runningcodegen-toprojectoption does not generate code. It creates only the project file.

Generate code frommyadd_project.prjby using anothercodegencommand.

codegenmyadd_project.prj

The code generator produces a C++ static library functionmyaddin thework\codegen\lib\myaddfolder, whereworkis your current working directory.

Example: Convert an IncompletecodegenCommand to a Template Project File

Create acoder.CodeConfigobject for generating a static library. SetTargetLangto'C++'.

cfg = coder.config('lib'); cfg.TargetLang ='C++';

At the MATLAB command line, create and run acodegencommand. Specifycfgas the code configuration object. Include the-toprojectoption to convert thecodegencommand to an equivalentMATLAB Coderproject file with namemyProjectTemplate.prj.

codegen-configcfg-toprojectmyProjectTemplate.prj
Project file 'myProjectTemplate.prj' was successfully created.Open Project

You can now openmyProjectTemplate.prjin theMATLAB Coderapp and use it as a template to create full project files that you can use to generate code.

Limitations

When you use thecodegencommand with the-toprojectoption, these limitations apply:

  • 间g theCodeTemplateparameter of acoder.EmbeddedCodeConfigobject to a project file is not supported.

  • Suppose that yourcodegencommand for generating a MEX function usescoder.Constantto define a constant input that is afi(Fixed-Point Designer)objectobj.

    Certainfiobject properties are enabled by other properties. When you construct afiobject, 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. Seefi对象属性(Fixed-Point Designer).

    You convert thiscodegencommand to a project file by using the-toproject选择。You build the project file and generate a MEX function. When you passobjas 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 ofobjthat are not enabled to their default values before passing it to the MEX function. To do this, define a newfiobjectobj_new:

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

    Passobj_newas the constant input to the generated MEX function.

See Also

Related Topics