Main Content

getSyntaxColoredCode

Class: mlreportgen.report.MATLABCode
Namespace: mlreportgen.report

Returns a DOM object containing a syntax-highlighted rendition of the code generated by the MATLABCode reporter

Since R2024b

Syntax

syntaxColoredCode = mlreportgen.report.MATLABCode.getSyntaxColoredCode(reporter,report)

Description

syntaxColoredCode = mlreportgen.report.MATLABCode.getSyntaxColoredCode(reporter,report) creates a DOM object containing a syntax-highlighted rendition of the code specified by reporter in the report specified by report.

Input Arguments

expand all

MATLAB code reporter, specified as an mlreportgen.report.MATLABCode object.

Report, specified as a report object. To create the report input, use mlreportgen.report.Report.

Output Arguments

expand all

Syntax-highlighted code, returned as a DOM object. The table shows the type of DOM object returned for each source specified for the MATLABCode reporter and the report output type.

MATLAB Code SourceReport TypeDOM Object
.mlxdocxmlreportgen.dom.EmbeddedObject
.mlx
  • html

  • html-file

mlreportgen.dom.RawText
.mlxpdfmlreportgen.dom.HTML
.m
  • html

  • html-file

  • docx

  • pdf

mlreportgen.dom.HTMLFile
Content specified directly using the Content property
  • html

  • html-file

  • docx

  • pdf

mlreportgen.dom.HTMLFile

Examples

expand all

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 R2024b