Generate Custom File and Function Banners for C/C++ Code
When you generate C and C++ code from MATLAB® code, you can use a code generation template (CGT) file to specify custom:
File banners
Function Banners
File trailers
Comments before code sections
This example shows how you can create your own CGT file and customize it to generate your own file and function banners.
Create a local copy of the default CGT file for MATLAB Coder™ and rename it. The default CGT file is
matlabcoder_default_template.cgt
in the
folder.matlabroot
/toolbox/coder/matlabcoder/templates/Store the copy in a folder that is outside of the MATLAB folder structure, but on the MATLAB path. If necessary, add the folder to the MATLAB path. If you intend to use the CGT file with a custom target, locate the CGT file in a folder under your target root folder. If the file is not on the MATLAB path, specify a full path to the file when adding the file to your configuration.
View the default template and generated output. For example, here is the default File Banner section:
When you generate code using this default, the file banner looks similar to this file banner:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Custom File Banner section (optional) %% Customize File banners by using either custom tokens or the following %% predefined tokens: %% %<FileName>, %<MATLABCoderVersion>, %<EmbeddedCoderVersion> %% %<SourceGeneratedOn>, %<HardwareSelection>, %<OutputType> %% %% You can also use "custom tokens" in all of the sections below. See the %% documentation center for more details. %% <FileBanner style="classic"> File: %<FileName> MATLAB Coder version : %<MATLABCoderVersion> C/C++ source code generated on : %<SourceGeneratedOn> </FileBanner>
/* * File: coderand.c * * MATLAB Coder version : 2.7 * C/C++ source code generated on : 06-Apr-2014 14:34:15 */
Edit your local copy of the CGT file. You can change the default values and add your own custom tokens. For example, here is the File Banner section with the style changed to
box
and a custom tokenmyCustomToken
:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Custom File Banner section (optional) %% Customize File banners by using either custom tokens or the following %% predefined tokens: %% %<FileName>, %<MATLABCoderVersion>, %<EmbeddedCoderVersion> %% %<SourceGeneratedOn>, %<HardwareSelection>, %<OutputType> %% %% You can also use "custom tokens" in all of the sections below. See the %% documentation center for more details. %% <FileBanner style="box"> File: %<FileName> My custom token : %<myCustomToken> MATLAB Coder version : %<MATLABCoderVersion> C/C++ source code generated on : %<SourceGeneratedOn> </FileBanner>
For more information, see Code Generation Template Files for MATLAB Code.
Create a configuration object for generation of a C static library for an embedded target.
% Create configuration object for an embedded target cfgObj = coder.config('lib','ecoder',true);
Create a
MATLABCodeTemplate
object from your CGT file and add it to the configuration object.% Specify the custom CGT file CGTFile = 'myCGTFile.cgt'; % Use custom template cfgObj.CodeTemplate = coder.MATLABCodeTemplate(CGTFile);
Assign values for custom tokens that you added to the template. For example, assign the value
'myValue'
to themyCustomToken
token that you added in a previous step.cfgObj.CodeTemplate.setTokenValue('myCustomToken','myValue');
Make sure that generation of comments is enabled. Otherwise, the code generator does not produce file or function banners.
cfgObj.GenerateComments=true;
Generate code using the configuration object that you created.
codegen -config cfgObj coderand
View the changes to the generated file banner. For example, here is the file banner for
coderand.c
using the customized CGT file:/******************************************************************************/ /* File: coderand.c */ /* */ /* My custom token : myValue */ /* */ /* MATLAB Coder version : 2.7 */ /* C/C++ source code generated on : 06-Apr-2014 14:42:55 */ /******************************************************************************/
Changes to a CGT file do not affect
the generated code unless you create a MATLABCodeTemplate
object
from the modified CGT file, and then add it to the configuration object.
If you modify the CGT File, myCGTFile.cgt
, used
in the previous example, you must repeat these steps:
Create a
MATLABCodeTemplate
object frommyCGTFile.cgt
and add it to the configuration object.CGTFile = 'myCGTFile.cgt'; cfgObj.CodeTemplate = coder.MATLABCodeTemplate(CGTFile);
Assign the value
'myValue'
to themyCustomToken
token.cfgObj.CodeTemplate.setTokenValue('myCustomToken','myValue');
Generate code.
codegen -config cfgObj coderand