Main Content

fix

Fix code issues

Since R2023a

    Description

    example

    [status,results] = fix(issues,checkID) 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,results] = fix(issues,checkID,filenames) fixes the specified issues for the files in filenames. Files contained in filenames must be included in the Files property of issues.

    example

    [status,results] = fix(issues,issuesTable) fixes the issues in issuesTable.

    Examples

    collapse all

    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:18: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>
    

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

    First, identify issues in the files test1.m and test2.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>
    

    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:18: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 the Issues property of issues as the input for fix.

    [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

    Code issues, specified as a codeIssues object.

    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"]

    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"

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

    Output Arguments

    collapse all

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

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

    Column NameColumn Purpose
    SuccessIndicate if issue was fixed, returned as true or false
    ErrorMessageDescribes why issue was not fixed; empty string if issue was fixed.
    FullFilenameFull path and filename of location of issue
    CheckIDCheck identifier used to find this code issue
    LineStartLine number where issue begins
    LineEndLine number where issue ends
    ColumnStartColumn number where issue begins
    ColumnEndColumn number where issue ends
    ErrorIDIdentifier for the error

    Version History

    Introduced in R2023a