fix - Fix code issues - MATLAB (original) (raw)

Fix code issues

Since R2023a

Syntax

Description

[[status](#mw%5Faf6d6745-774a-472d-a910-38fc956a593e),[results](#mw%5F9f451f9b-a657-4715-927c-94a1fe312c89)] = fix([issues](#mw%5Fdea40832-b9fd-4e00-a753-16884b7525f5),[checkID](#mw%5Fb9450e65-20c0-4960-b6f2-1fbc32abc4f4)) fixes the checkID issues found in issues and returns status, a codeIssues object containing any remaining issues, and results, a table that describes the fix results.

example

[[status](#mw%5Faf6d6745-774a-472d-a910-38fc956a593e),[results](#mw%5F9f451f9b-a657-4715-927c-94a1fe312c89)] = fix([issues](#mw%5Fdea40832-b9fd-4e00-a753-16884b7525f5),[checkID](#mw%5Fb9450e65-20c0-4960-b6f2-1fbc32abc4f4),[filenames](#mw%5F264f0bc4-6e45-4fe9-b6e2-6594d2a03375)) fixes the specified issues for the files in filenames. Files contained in filenames must be included in the Files property of issues.

example

[[status](#mw%5Faf6d6745-774a-472d-a910-38fc956a593e),[results](#mw%5F9f451f9b-a657-4715-927c-94a1fe312c89)] = fix([issues](#mw%5Fdea40832-b9fd-4e00-a753-16884b7525f5),[issuesTable](#mw%5F784e6dfc-3f8b-4112-a9bb-51a7eccef2bd)) fixes the issues in issuesTable.

example

Examples

collapse all

Fix Issue in Code

Identify and fix issues in the file test.m by using the fix object function for codeIssues.

First, identify issues in the file test.m by using codeIssues.

issues = codeIssues("test.m")

issues =

codeIssues with properties:

                     Date: 18-Oct-2022 14🔞54
                  Release: "R2023a"
                    Files: "C:\MyCode\test.m"
CodeAnalyzerConfiguration: "active"
                   Issues: [3×10 table]
         SuppressedIssues: [0×11 table]


Issues table preview

Location    Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename   
________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    __________________

"test.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."    AGROW          3           3            1             3        "C:\MyCode\test.m"
"test.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                         NOPTS          6           6            3             3        "C:\MyCode\test.m"
"test.m"      info        auto        "string('...') is not recommended. Use "..." instead."                                          STRQUOT        8           8            1            13        "C:\MyCode\test.m"

Fix the STRQUOT issue by using fix.

[status,results] = fix(issues,"STRQUOT")

status =

codeIssues with properties:

                     Date: 18-Oct-2022 14:21:28
                  Release: "R2023a"
                    Files: "C:\MyCode\test.m"
CodeAnalyzerConfiguration: "active"
                   Issues: [3×10 table]
         SuppressedIssues: [0×11 table]


Issues table preview

Location    Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename   
________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    __________________

"test.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."     AGROW         3           3            1             3        "C:\MyCode\test.m"
"test.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                          NOPTS         6           6            3             3        "C:\MyCode\test.m"

results =

1×9 table

Success    ErrorMessage       FullFilename       CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd     ErrorID 
_______    ____________    __________________    _______    _________    _______    ___________    _________    _________

 true       <missing>      "C:\MyCode\test.m"    STRQUOT        8           8            1            13        <missing>

Fix Issue in Specific Files

Identify issues in the files test1.m andtest2.m and apply fixes to only one file using thefix object function for codeIssues.

First, identify issues in the files test1.m andtest2.m by using codeIssues.

issues = codeIssues(["test1.m" "test2.m"])

issues =

codeIssues with properties:

                     Date: 07-Dec-2022 10:52:15
                  Release: "R2023a"
                    Files: [2×1 string]
CodeAnalyzerConfiguration: "active"
                   Issues: [5×10 table]
         SuppressedIssues: [0×11 table]


Issues table preview

Location     Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename    
_________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    ___________________

"test1.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."    AGROW          3           3            1             3        "C:\MyCode\test1.m"
"test1.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                         NOPTS          6           6            3             3        "C:\MyCode\test1.m"
"test1.m"      info        auto        "string('...') is not recommended. Use "..." instead."                                          STRQUOT        8           8            1            13        "C:\MyCode\test1.m"
"test2.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                         NOPTS          3           3            3             3        "C:\MyCode\test2.m"
"test2.m"      info        auto        "string('...') is not recommended. Use "..." instead."                                          STRQUOT        5           5            1            13        "C:\MyCode\test2.m"

Fix the STRQUOT issue by using fix, but only in the file test1.m.

[status,results] = fix(issues,"STRQUOT","test1.m")

status =

codeIssues with properties:

                     Date: 07-Dec-2022 10:54:35
                  Release: "R2023a"
                    Files: [2×1 string]
CodeAnalyzerConfiguration: "active"
                   Issues: [5×10 table]
         SuppressedIssues: [0×11 table]


Issues table preview

Location     Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename    
_________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    ___________________

"test1.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."    AGROW          3           3            1             3        "C:\MyCode\test1.m"
"test1.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                         NOPTS          6           6            3             3        "C:\MyCode\test1.m"
"test2.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                         NOPTS          3           3            3             3        "C:\MyCode\test2.m"
"test2.m"      info        auto        "string('...') is not recommended. Use "..." instead."                                          STRQUOT        5           5            1            13        "C:\MyCode\test2.m"

results =

1×9 table

Success    ErrorMessage       FullFilename        CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd     ErrorID 
_______    ____________    ___________________    _______    _________    _______    ___________    _________    _________

 true       <missing>      "C:\MyCode\test1.m"    STRQUOT        8           8            1            13        <missing>

Fix Multiple Issues in Code

Identify and fix issues in the file test.m by using the fix object function for codeIssues.

First, identify issues in the file test.m by usingcodeIssues.

issues = codeIssues("test.m")

issues =

codeIssues with properties:

                     Date: 18-Oct-2022 14🔞54
                  Release: "R2023a"
                    Files: "C:\MyCode\test.m"
CodeAnalyzerConfiguration: "active"
                   Issues: [3×10 table]
         SuppressedIssues: [0×11 table]


Issues table preview

Location    Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename   
________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    __________________

"test.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."    AGROW          3           3            1             3        "C:\MyCode\test.m"
"test.m"      info        auto        "Add a semicolon after the statement to hide the output (in a script)."                         NOPTS          6           6            3             3        "C:\MyCode\test.m"
"test.m"      info        auto        "string('...') is not recommended. Use "..." instead."                                          STRQUOT        8           8            1            13        "C:\MyCode\test.m"

Fix the issues found in test.m. Use the table in theIssues property of issues as the input forfix.

[status,results] = fix(issues,issues.Issues)

status =

codeIssues with properties:

                     Date: 07-Dec-2022 10:37:36
                  Release: "R2023a"
                    Files: "C:\MyCode\test.m"
CodeAnalyzerConfiguration: "active"
                   Issues: [2×10 table]
         SuppressedIssues: [0×11 table]


Issues table preview

Location    Severity    Fixability                                            Description                                             CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd       FullFilename   
________    ________    __________    ____________________________________________________________________________________________    _______    _________    _______    ___________    _________    __________________

"test.m"      info        manual      "Variable appears to change size on every loop iteration. Consider preallocating for speed."     AGROW         3           3            1             3        "C:\MyCode\test.m"

results =

3×9 table

Success                                 ErrorMessage                                    FullFilename       CheckID    LineStart    LineEnd    ColumnStart    ColumnEnd                   ErrorID               
_______    ______________________________________________________________________    __________________    _______    _________    _______    ___________    _________    _____________________________________

 false     "The issue does not have an available fix in file 'C:\MyCode\test.m'."    "C:\MyCode\test.m"    AGROW          3           3            1             3        "MATLAB:codeanalyzer:CheckIDHasNoFix"
 true      <missing>                                                                 "C:\MyCode\test.m"    NOPTS          6           6            3             3        <missing>                            
 true      <missing>                                                                 "C:\MyCode\test.m"    STRQUOT        8           8            1            13        <missing>                            

Input Arguments

collapse all

issues — Code issues

codeIssues object

Code issues, specified as a codeIssues object.

checkID — Issue check ID

character vector | string array | cell array of character vectors

Issue check identifier, specified as a character vector or string array. For a complete list of check identifiers, see Index of Code Analyzer Checks.

Example: "STRQUOT"

Example: ["STRQUOT" "AGROW"]

filenames — Files to analyze

character vector | string array | cell array of character vectors

Files to fix, specified as a character vector, string array, cell array of character vectors. A filename can include the full, relative, or partial path. The name of a file must be a valid MATLAB® code or app file (.m, .mlx, or.mlapp).

Example: "../myFile.m"

Example: ["file1.mlx" "file2.mlapp"]

Example: "C:\MyCode\test.m"

issuesTable — Table of issues

codeIssues.Issues table

Table of issues, specified as a codeIssues.Issues table.

Output Arguments

collapse all

status — Code status

codeIssues object

Code status, returned as a codeIssues object containing information on the status of the code after the function has applied fixes.

results — Fix results

table

Fix results, returned as a table with a row for each issue included incheckID or issuesTable and with these columns:

Column Name Column Purpose
Success Indicate if issue was fixed, returned as true orfalse
ErrorMessage Describes why issue was not fixed; empty string if issue was fixed.
FullFilename Full path and filename of location of issue
CheckID Check identifier used to find this code issue
LineStart Line number where issue begins
LineEnd Line number where issue ends
ColumnStart Column number where issue begins
ColumnEnd Column number where issue ends
ErrorID Identifier for the error

Version History

Introduced in R2023a