mlreportgen.report.MATLABCode Class
Namespace: mlreportgen.report
Superclasses: mlreportgen.report.Reporter
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 arguments. You can specify multiple name-value arguments in any order.
Properties
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:
| GetAccess | public | 
| SetAccess | public | 
| 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:
| GetAccess | public | 
| SetAccess | public | 
| NonCopyable | true | 
Data Types: char | string
Whether to apply smart indenting to the code, specified as true
            or false.
Attributes:
| GetAccess | public | 
| SetAccess | public | 
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. 
Attributes:
| GetAccess | public | 
| SetAccess | public | 
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.
Attributes:
| GetAccess | public | 
| SetAccess | public | 
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 this reporter uses or whose template library contains the template for this reporter 
- Document Object Model (DOM) document or document part whose template this reporter uses 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.
Attributes:
| GetAccess | public | 
| SetAccess | public | 
| NonCopyable | true | 
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.
Attributes:
| GetAccess | public | 
| SetAccess | public | 
Data Types: char | string
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.
Attributes:
| GetAccess | public | 
| SetAccess | public | 
Methods
| Method | Purpose | 
|---|---|
| getSyntaxColoredCode | Returns a DOM object containing a syntax-highlighted rendition of the code generated by the MATLABCodereporter | 
| mlreportgen.report.MATLABCode.createTemplate | 
 Create reporter template Input Arguments 
 Return Values 
 For more information, see the equivalent method for the  | 
| mlreportgen.report.MATLABCode.customizeReporter | 
 Create reporter derived from  Return Values 
 For more information, see the equivalent method for the  | 
| mlreportgen.report.MATLABCode.getClassFolder | 
 Get location of folder that contains
         Return Values 
 For more information, see the equivalent method for the  | 
| copy | Create copy of reporter object and make deep copies of certain property values | 
| getImpl | Get implementation of reporter | 
Examples
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 | 
 | 
| .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 R2021aUse 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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)