mlreportgen.report.MATLABCode Class
Namespace: mlreportgen.report
Description
Use an object of the mlreportgen.report.MATLABCode
class to include
syntax-highlighted MATLAB® code in a report. If you need a DOM object instead
of a Report object, use the method getSyntaxColoredCode
with the mlreportgen.report.MATLABCode
object. (since R2024b)
The mlreportgen.report.MATLABCode
class is a handle
class.
Creation
Description
reporter = mlreportgen.report.MATLABCode(filename)
creates a MATLABCode
reporter with the FileName
property set to filename
.
reporter = mlreportgen.report.MATLABCode(Name=Value)
sets properties using name-value pairs. You can specify multiple name-value pair arguments
in any order.
Properties
FileName
— Path and file name of MATLAB code file
[]
(default) | character vector | string scalar
Path and file name of the file that contains MATLAB code, specified as a character vector or string scalar. The file can have
a .m
or .mlx
extension. If you set this property,
the MATLABCode
reporter sets the Content
property to a string scalar that contains the code contained in the specified
file.
Attributes:
NonCopyable | true |
Data Types: char
| string
Content
— MATLAB code
[]
(default) | character vector | string scalar
MATLAB code, specified as a character vector or string scalar. Set this property
only if the FileName
property is not set.
Attributes:
NonCopyable | true |
Data Types: char
| string
SmartIndent
— Whether to apply smart indenting to the code
false
(default) | true
Whether to apply smart indenting to the code, specified as true
or false
.
Data Types: logical
IncludeComplexity
— Whether to include code complexity
false
(default) | true
Whether to include code complexity, specified as true
or
false
. If the value is true
, the report includes
the McCabe cyclomatic complexity of each function that the MATLAB code contains.
Data Types: logical
ComplexityReporter
— Code complexity reporter
mlreportgen.report.BaseTable
Code complexity reporter, specified as an mlreportgen.report.BaseTable
object. The BaseTable
reporter is
used to report and format the code complexity tabular data. The default value of this
property is a BaseTable
object with the
TableStyleName
property set to
"MATLABCodeTable"
and the other properties set to default values.
You can customize the appearance of the table by customizing the default reporter or by
replacing it with a custom BaseTable
reporter. Any content that you
specify in the Title
property of the default or the replacement
BaseTable
reporter appears before the title in the generated
report.
TemplateSrc
— Source of template for this reporter
character vector | string scalar | reporter or report | DOM document or document part
Source of the template for this reporter, specified in one of these ways:
Character vector or string scalar that specifies the path of the file that contains the template for this reporter
Reporter or report whose template is used for this reporter or whose template library contains the template for this reporter
Document Object Model (DOM) document or document part whose template is used for this reporter or whose template library contains the template for this reporter
The specified template must be the same type as the report to which you
append this reporter. For example, for a Microsoft® Word report, TemplateSrc
must be a Word reporter template.
If the TemplateSrc
property is empty, this reporter uses the
default reporter template for the output type of the report.
TemplateName
— Name of template for this reporter
"MATLABCode"
(default) | character vector | string scalar
Name of the template for this reporter, specified as a character vector or string
scalar. The template for this reporter must be in the template library of the template
source (TemplateSrc
) for this reporter.
LinkTarget
— Hyperlink target for this reporter
[]
(default) | character vector | string scalar | mlreportgen.dom.LinkTarget
object
Hyperlink target for this reporter, specified as a character vector or string scalar
that specifies the link target ID, or an mlreportgen.dom.LinkTarget
object. A character vector or string scalar
value converts to a LinkTarget
object. The link target immediately
precedes the content of this reporter in the output report.
Methods
Public Methods
mlreportgen.report.MATLABCode.createTemplate | Create copy of mlreportgen.report.MATLABCode reporter
template |
mlreportgen.report.MATLABCode.customizeReporter | Create subclass of mlreportgen.report.MATLABCode
class |
mlreportgen.report.MATLABCode.getClassFolder | Get location of folder that contains
mlreportgen.report.MATLABCode class definition file |
getSyntaxColoredCode | Returns a DOM object containing a syntax-highlighted rendition of the code generated
by the MATLABCode reporter |
copy | Create copy of reporter object and make deep copies of certain property values |
getImpl | Get implementation of reporter |
Examples
Include a Syntax-Highlighted MATLAB Function in a Report
Report the syntax-highlighted code for the function myAdd.m
.
Create a report.
import mlreportgen.dom.* import mlreportgen.report.* rpt = Report("MyReport","pdf");
Create a chapter.
chap = Chapter("The myAdd Function");
Create a MATLABCode
reporter to report on the content of myAdd.m
.
mCode = MATLABCode("myAdd.m");
Add the reporter to the chapter and the chapter to the report. If you need to add syntax-highlighted MATLAB code to a DOM object, see Add Syntax-Highlighted MATLAB Code DOM Object to Report.
append(chap,mCode); append(rpt,chap);
Close the report and open the viewer.
close(rpt); rptview(rpt);
Here is the syntax-highlighted code in the report.
Add Syntax-Highlighted MATLAB Code DOM Object to Report
Use the getSyntaxColoredCode
method to create a DOM object from the code read by a MATLABCode
reporter. getSyntaxColoredCode
returns a different DOM object type depending on the report format and the source of the MATLAB code.
MATLAB Code Source | Report Format | DOM object |
---|---|---|
.mlx | DOCX |
|
.mlx | HTML HTML-FILE |
|
.mlx |
| |
.m | DOCX HTML HTML-FILE |
|
Content specified directly using the | DOCX HTML HTML-FILE |
|
import mlreportgen.dom.* import mlreportgen.report.*
Create a report.
rpt = Report("MyReport","pdf");
Create a chapter.
chap = Chapter("The myAdd Function");
Create a DOM table.
table = mlreportgen.dom.Table(2); table.Style = {... Border("solid"),... ColSep("solid"),... RowSep("solid"),... Width("100%")... }; table.TableEntriesVAlign = "middle";
Create a table row.
tr = TableRow();
Create the first table entry and append some content to it.
entry11 = TableEntry();
append(entry11,"Addition");
Create another table entry for the MATLAB code.
entry12 = TableEntry();
Create a MATLABCode
reporter to report on the content of myAdd.m
.
mCode = MATLABCode("myAdd.m");
Create a DOM object from the reporter results.
synatxColorCode = getSyntaxColoredCode(mCode,rpt)
synatxColorCode = HTMLFile with properties: KeepInterElementWhiteSpace: 0 EMBaseFontSize: 12 HTMLTag: 'div' StyleName: [] Style: {1×0 cell} CustomAttributes: [] Parent: [] Children: [1×1 mlreportgen.dom.Container] Tag: 'dom.HTMLFile:50558' Id: '50558'
Add the syntax-color rendition of the code to the table entry.
append(entry12,synatxColorCode);
Append both the table entries to the table row and the table row to the table.
append(tr,entry11); append(tr,entry12); append(table,tr);
Add table to the chapter and the chapter to the report.
append(chap,table); append(rpt,chap);
Close the report and open the viewer.
close(rpt); rptview(rpt);
Version History
Introduced in R2021aR2024b: Add MATLAB syntax-highlighted content to a report
Use the method getSyntaxColoredCode
to programmatically add MATLAB syntax-highlighted content as a DOM object to a report. The method generates
the DOM object appropriate for the report output type from a specified
.mlx
or .m
file.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)