export - Write code issues to file - MATLAB (original) (raw)
Write code issues to file
Since R2023b
Syntax
Description
export([issues](#mw%5F164598d5-7c1b-4167-b304-396e48a10028))
writes the contents of the specified codeIssues object to the file codeIssues.json
.
export([issues](#mw%5F164598d5-7c1b-4167-b304-396e48a10028),[filename](#mw%5F45416429-073f-404e-9465-f8aa66d89718))
writes contents of issues
to the specified file.
export(___,[Name=Value](#namevaluepairarguments))
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
Export Code Issues to JSON File
Create a report containing issues found in code.
First, create a function myfun
in a file namedmyfun.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 usingexport
.
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 } } } ] } ] } ] }
Export Code Issues to Specified File
Create a report containing issues found in code and export to a specified file.
First, create a function myfun
in a file namedmyfun.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 namedmyReport
.
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
issues
— Code issues
codeIssues
object
Code issues, specified as a codeIssues object.
filename
— Name of output file
string scalar | character vector
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. Iffilename
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 asName1=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")
FileFormat
— Format of output file
string scalar | character vector
Format of output file, specified as a string scalar or character vector.FileFormat
must be one of these values:
Value | File Format |
---|---|
"auto" (default) | Format is determined by the file extension offilename. 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"
SourceRoot
— Source folder for analysis
string scalar | character vector
Source folder for analysis, specified as a string scalar or character vector containing the full or relative path to the root folder. You can specifySourceRoot
only if the output file format is SARIF.
Example: SourceRoot="\my\root\folder"
Version History
Introduced in R2023b