Main Content

mlreportgen.report.MATLABCode Class

Namespace: mlreportgen.report

MATLAB code reporter

Since R2021a

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.

Class Attributes

HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

reporter = mlreportgen.report.MATLABCode creates an empty MATLABCode reporter object based on the default template. You must specify the MATLAB code file by setting the FileName property, or specify the code content by setting the Content property. Use other properties to specify reporter options.

reporter = mlreportgen.report.MATLABCode(filename) creates a MATLABCode reporter with the FileName property set to filename.

example

reporter = mlreportgen.report.MATLABCode(Name=Value) sets properties using name-value pairs. You can specify multiple name-value pair arguments in any order.

Properties

expand all

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

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

Whether to apply smart indenting to the code, specified as true or false.

Data Types: logical

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

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.

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.

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.

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

expand all

Examples

collapse all

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.

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

mlreportgen.dom.EmbeddedObject

.mlx

HTML

HTML-FILE

mlreportgen.dom.RawText

.mlx

PDF

mlreportgen.dom.HTML

.m

DOCX

HTML

HTML-FILE

PDF

mlreportgen.dom.HTMLFile

Content specified directly using the Content property

DOCX

HTML

HTML-FILE

PDF

mlreportgen.dom.HTMLFile

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 R2021a

expand all