MCG - A Matlab Code Generation Engine

MCG is an in-place code generation engine. It is a Matlab knock off of Cog by Ned Batchelder.
472 Downloads
Updated 10 Apr 2011

View License

MCG is a tool for doing in-place code generation. MCG is intended to
provide the maximum traceability of generated code back to the Matlab
code generator.

MCG processes a source file looking for sections of specially tagged
code. The tags that denote generation sections exist in comment
sections. These sections are expected to contain Matlab code. MCG
finds the code, executes it, then places text generated back into the
original source file.

Example:
Running MCG on itself will add a line of executable Matlab code in
this documentation header. Example code:

[[[MCG
mcg.outl('% --Example generated code!--');
]]]
[[[/MCG]]]

To run MCG:
>> m = mcg;
>> m.codegen('mcg.m');

After running the example above, the MCG source will have an additional
line inserted between the "]]]" and "[[[/MCG]]]" lines. Note that MCG
ships with both C and LaTeX source code generation examples.

The code generation section is denoted by two tags: "[[[MCG]]]" which
begins the section, and "[[[/MCG]]]" which ends the section. Each tag is
started by "[[[" and ended by "]]]". All code generated by the section
is placed between the section start and end tags.

All tags must be on lines which are commented out. MCG only supports
single line, not block, comments. MCG automatically determines the
comment characters being used and is thus language independent.

The section opening tag contains the Matlab code to run in order to
generate code. Any amount of valid Matlab code may exist on the lines
between "[[[MCG" and "]]]", but the "[[[MCG" and "]]]" must be on their
own lines. In addition, the section close tag "[[[/MCG]]]" must be on
its own line.

Generated code is sent to the source file by calling the static method:
MCG.OUTL. This method will append a carriage return, or a carriage
return + linefeed to the end of each line it has been passed. The
correct line ending is determined automatically.

Each block of code is executed in it's own namespace. Variables are not
retained between block calls. The MCG object provides a USERDATA
property which is accessible from the executing instance within the
code being executed. This property can be used to pass data between
different code generation sections.

The currently executing instance may be retrieved by the code being
executed by calling the method: MCG.INSTANCE.

MCG tag start and end characters can be customized, as can the section
start and end characters. Care should be taken that the tag and section
characters are not common to the language of the source file to reduce
confusion.

Code blocks are handled in the order in which they exist in the source
file. Files are processed in the order in which they are passed.

MCG is a Matlab knock off of Cog by Ned Batchelder. See:
http://nedbatchelder.com/code/cog/index.html#h_Writing_the_source_files

Cite As

Doug Harriman (2024). MCG - A Matlab Code Generation Engine (https://www.mathworks.com/matlabcentral/fileexchange/31009-mcg-a-matlab-code-generation-engine), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Code Generation in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Version Published Release Notes
1.0.0.0