Main Content

export

Write code issues to file

Since R2023b

    Description

    export(issues) writes the contents of the specified codeIssues object to the file codeIssues.json.

    example

    export(issues,filename) writes contents of issues to the specified file.

    example

    export(___,Name=Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example, you can specify the format of the output file.

    Examples

    collapse all

    Create a report containing issues found in code.

    First, create a function myfun in a file named myfun.m.

    function x = myfun
    randomNum = rand
    for n = 1:10
        x(n) = sin(randomNum);
    end
    end 

    Identify code issues in myfun by using codeIssues.

    C = codeIssues("myfun.m")
    C = 
    
      codeIssues with properties:
    
                             Date: 28-Apr-2023 10:49:26
                          Release: "R2023b"
                            Files: "C:\MyCode\myfun.m"
        CodeAnalyzerConfiguration: "active"
                           Issues: [2×10 table]
                 SuppressedIssues: [0×11 table]
    
    
        Issues table preview
    
        Location     Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename    
        _________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    ___________________
    
        "myfun.m"      info        auto        "Add a semicolon after the statement to hide the output (in a function)."                        NOPRT         2           2           11            11        "C:\MyCode\myfun.m"
        "myfun.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."     AGROW         4           4            8             8        "C:\MyCode\myfun.m"
    

    Export the issues found in myfun by using export.

    export(C)

    Open codeIssues.sarif to see the exported report.

    {
      "version": "2.1.0",
      "properties": {
        "Date": "28-Apr-2023 10:49:26",
        "Release": "R2023b",
        "CodeAnalyzerConfiguration": "active"
      },
      "runs": [
        {
          "tool": {
            "driver": {
              "name": "Code Analyzer"
            }
          },
          "artifacts": {
            "location": {
              "uri": "file:///C:/MyCode/myfun.m"
            }
          },
          "results": [
            {
              "ruleId": "NOPRT",
              "level": "note",
              "message": {
                "text": "Add a semicolon after the statement to hide the output (in a function)."
              },
              "locations": [
                {
                  "physicalLocation": {
                    "artifactLocation": {
                      "uri": "file:///C:/MyCode/myfun.m"
                    },
                    "region": {
                      "startLine": 2,
                      "endLine": 2,
                      "startColumn": 11,
                      "endColumn": 12
                    }
                  }
                }
              ]
            },
            {
              "ruleId": "AGROW",
              "level": "note",
              "message": {
                "text": "Variable appears to change size on every loop iteration. Consider preallocating for speed."
              },
              "locations": [
                {
                  "physicalLocation": {
                    "artifactLocation": {
                      "uri": "file:///C:/MyCode/myfun.m"
                    },
                    "region": {
                      "startLine": 4,
                      "endLine": 4,
                      "startColumn": 8,
                      "endColumn": 9
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
    

    Create a report containing issues found in code and export to a specified file.

    First, create a function myfun in a file named myfun.m.

    function x = myfun
    randomNum = rand
    for n = 1:10
        x(n) = sin(randomNum);
    end
    end 

    Identify code issues in myfun by using codeIssues.

    C = codeIssues("myfun.m")
    C = 
    
      codeIssues with properties:
    
                             Date: 25-Apr-2023 10:29:59
                          Release: "R2023b"
                            Files: "C:\MyCode\myfun.m"
        CodeAnalyzerConfiguration: "active"
                           Issues: [2×10 table]
                 SuppressedIssues: [0×11 table]
    
    
        Issues table preview
    
        Location     Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename    
        _________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    ___________________
    
        "myfun.m"      info        auto        "Add a semicolon after the statement to hide the output (in a function)."                        NOPRT         2           2           11            11        "C:\MyCode\myfun.m"
        "myfun.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."     AGROW         4           4            8             8        "C:\MyCode\myfun.m"
    
    

    Export the issues found in myfun using to a file named myReport.

    export(C,"myReport")

    Open myReport.sarif to see the exported report.

    {
      "version": "2.1.0",
      "properties": {
        "Date": "28-Apr-2023 10:49:26",
        "Release": "R2023b",
        "CodeAnalyzerConfiguration": "active"
      },
      "runs": [
        {
          "tool": {
            "driver": {
              "name": "Code Analyzer"
            }
          },
          "artifacts": {
            "location": {
              "uri": "file:///C:/MyCode/myfun.m"
            }
          },
          "results": [
            {
              "ruleId": "NOPRT",
              "level": "note",
              "message": {
                "text": "Add a semicolon after the statement to hide the output (in a function)."
              },
              "locations": [
                {
                  "physicalLocation": {
                    "artifactLocation": {
                      "uri": "file:///C:/MyCode/myfun.m"
                    },
                    "region": {
                      "startLine": 2,
                      "endLine": 2,
                      "startColumn": 11,
                      "endColumn": 12
                    }
                  }
                }
              ]
            },
            {
              "ruleId": "AGROW",
              "level": "note",
              "message": {
                "text": "Variable appears to change size on every loop iteration. Consider preallocating for speed."
              },
              "locations": [
                {
                  "physicalLocation": {
                    "artifactLocation": {
                      "uri": "file:///C:/MyCode/myfun.m"
                    },
                    "region": {
                      "startLine": 4,
                      "endLine": 4,
                      "startColumn": 8,
                      "endColumn": 9
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
    

    Input Arguments

    collapse all

    Code issues, specified as a codeIssues object.

    Name of output file, specified as a string scalar or character vector. If filename does not include a file extension, export uses the SARIF file format. If filename includes a valid file extension, export uses that extension to determine the file format. For a list of valid formats, see FileFormat.

    Example: "myFile.sarif"

    Example: "myFile.json"

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: export(issues,"issuesFile.sarif",FileFormat="sarif",SourceRoot="\my\root\folder")​​

    Format of output file, specified as a string scalar or character vector. FileFormat must be one of these values:

    ValueFile Format
    "auto" (default)Format is determined by the file extension of filename. If filename does not include an extension, then .sarif is the default extension.
    "json"Exported as JSON file using the format of jsonencode
    "sarif"Exported as JSON file using the format of SARIF
    "sonarqube"Exported as JSON file using the format of SonarQube

    Example: FileFormat="sarif"

    Source folder for analysis, specified as a string scalar or character vector containing the full or relative path to the root folder. You can specify SourceRoot only if the output file format is SARIF.

    Example: SourceRoot="\my\root\folder"​​

    Version History

    Introduced in R2023b