Main Content

MATLAB Code

himl_0004: MATLAB Code Analyzer recommendations for code generation

ID: Titlehiml_0004: MATLAB® Code Analyzer recommendations for code generation
DescriptionWhen using MATLAB code:
ATo activate MATLAB Code Analyzer messages for code generations, use the %#codegen directive in external MATLAB functions.
B

Review the MATLAB Code Analyzer messages. Either:

  • Implement the recommendations or

  • Justify not following the recommendations with %#ok<message-ID(S)> directives in the MATLAB function. Do not use %#ok without specific message-IDs.

Notes

The MATLAB Code Analyzer messages provide identifies potential errors, problems, and opportunities for improvement in the code.

RationaleAIn external MATLAB functions, the %#codegen directive activates MATLAB Code Analyzer messages for code generation.
B
  • Following MATLAB Code Analyzer recommendations helps to:

    • Generate efficient code.

    • Follow best code generation practices

    • Avoid using MATLAB features not supported for code generation.

    • Avoid code patterns which potentially influence safety.

  • Not following MATLAB Code Analyzer recommendations are justified with message id (e.g. %#ok<NOPRT>.

    In the MATLAB function, using %#ok without a message id justifies the full line, potentially hiding issues.

Model Advisor ChecksCheck MATLAB Code Analyzer messages (Simulink Check)
References
  • IEC 61508-3, Table A.3 (3) 'Language subset’
    IEC 61508-3, Table A.4 (3) 'Defensive programming’
    IEC 61508-3, Table A.4 (5) 'Design and coding standards'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'
    ISO 26262-6, Table 1 (1d) 'Use of defensive implementation techniques'
    ISO 26262-6, Table 1 (1e) 'Use of well-trusted design principles'
    ISO 26262-6, Table 1 (1f) 'Use of unambiguous graphical representation'
    ISO 26262-6, Table 1 (1g) 'Use of style guides'
    ISO 26262-6, Table 1 (1h) 'Use of naming conventions'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'
    EN 50128, Table A.12 (1) 'Coding Standard'
    EN 50128, Table A.12 (2) 'Coding Style Guide'

  • DO-331, Section MB.6.3.1.b 'Accuracy and consistency’
    DO-331, Section MB.6.3.2.b 'Accuracy and consistency’

See Also

Check Code for Errors and Warnings Using the Code Analyzer

Last ChangedR2016a
Examples

Recommended

  • Activate MATLAB Code Analyzer messages for code generations:

    %#codegen
      function y = function(u)
        y = inc_u(u));     
      end
      function yy = inc_u(uu)
        yy = uu + 1;
      end
    

  • Justify missing ; and value assigned might be unused:

    y = 2*u %#ok<NOPRT,NAGSU> output for debugging
    ...
    y = 3*u;

  • If output is not desired and assigned value is unused, remove the line y = 2*u ...:

    y = 3*u;

Not Recommended

  • External MATLAB file used in Simulink® with missing %#codegen directive:

    function y = function(u)
        % nested functions can't be used for code generation
        function yy = inc_u(uu)
          yy = uu + 1;
        end
        y = inc_u(u));     
      end
    

  • All messages in line are justified by using %#ok without a message ID:

    % missing ';' and the value might be unused
      y = 2*u %#ok 
      …
      y = 3*u;
    

  • No justification:

    % missing justification for missing ';' and unnecessary '[..]'
    y= [2*u]

himl_0006: MATLAB code if / elseif / else patterns

ID: Titlehiml_0006: MATLAB code if / elseif / else patterns
Description

For MATLAB code with if / elseif/ else constructs, terminate the constructs with an else statement that includes at least a meaningful comment. A final else statement is not required if there is no elseif.

Rationale
  • Defensive programming

  • Readability

  • Traceability

Model Advisor ChecksCheck if/elseif/else patterns in MATLAB Function blocks (Simulink Check)
References
  • IEC 61508-3, Table A.3 (3) 'Language subset’
    IEC 61508-3, Table A.4 (3) 'Defensive programming’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1(b) 'Use of language subsets'
    ISO 26262-6, Table 1(d) 'Use of defensive implementation techniques'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'

  • DO-331, Section MB.6.3.1.e 'Conformance to standards'
    DO-331, Section MB.6.3.2.e 'Conformance to standards'
    DO-331, Section MB.6.3.3.e 'Conformance to standards'

See Also
Last ChangedR2018b
Examples

Recommended

  • if u > 0
        y = 1;
      end
    

  • if u > 0
        y = 1;
      elseif u < 0
        y = -1;
      else
        y = 0;
      end
    

  • y = 0;
      if u > 0
        y = 1;
      elseif u < 0
    y = -1;
      else
        % handled before if
      end
    

Not Recommended

  •   % empty else
      y = 0;
      if u > 0
        y = 1;
      elseif u < 0
        y = -1;
      else
      end
    

  •   % missing else
      y = 0;
      if u > 0
        y = 1;
      elseif u < 0
        y = -1;
      end
    

himl_0007: MATLAB code switch / case / otherwise patterns

ID: Titlehiml_0007: MATLAB code switch / case / otherwise patterns
Description

For MATLAB code with switch statements, include:

  • At least two case statements.

  • An otherwise statement that at least includes a meaningful comment.

Note

If there is only one case and one otherwise statement, consider using an if / else statement.

Rationale
  • Defensive programming

  • Readability

  • Traceability

Model Advisor ChecksCheck switch statements in MATLAB Function blocks (Simulink Check)
References
  • IEC 61508-3, Table A.3 (3) 'Language subset’
    IEC 61508-3, Table A.4 (3) 'Defensive programming’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1(b) 'Use of language subsets'
    ISO 26262-6, Table 1(d) 'Use of defensive implementation techniques'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'

  • DO-331, Section MB.6.3.1.e 'Conformance to standards'
    DO-331, Section MB.6.3.2.e 'Conformance to standards'
    DO-331, Section MB.6.3.3.e 'Conformance to standards'

  • MISRA C:2012, Rule 16.4

See Also
Last ChangedR2018b
Examples

Recommended

  • switch u
        case 1
          y = 3;
        case 3
          y = 1;
        otherwise
          y = 1;
      end
    

  • y = 0;
      switch u
        case 1
          y = 3;
        case 3
          y = 1;
        otherwise
          % handled before switch
      end
    

Not Recommended

  •   % no case statements
      switch u
        otherwise
          y = 1;
      end
    

  •   % empty otherwise statement
      switch u
        case 1
          y = 3;
        case 3
          y = 1;
        otherwise
      end
    

  •   % no otherwise statement
      switch u
        case 1
          y = 3;
      end
    

himl_0008: MATLAB code relational operator data types

ID: Titlehiml_0008: MATLAB code relational operator data types
Description

For MATLAB code with relational operators, use the same data type for the left and right operands.

NoteIf the two operands have different data types, MATLAB will promote both operands to a common data type. This can lead to unexpected results.
Rationale
  • Prevent implicit casts

  • Prevent unexpected results

Model Advisor ChecksCheck usage of relational operators in MATLAB Function blocks (Simulink Check)
References
  • DO-331, Section MB.6.3.1.g 'Algorithms are accurate'

  • DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’

  • EC 61508-3, Table A.3 (3) 'Language subset’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1(1b) 'Use of language subsets'

  • ISO 26262-6, Table 1(1c) 'Enforcement of strong typing'

  • ISO 26262-6, Table 6 (1g) 'No implicit type conversions'

  • EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'

  • EN 50128, Table A.4 (11) 'Language Subset'

See Also
Last ChangedR2024a
Examples

Recommended

  • myBool == true
    myInt8 == int8(1) 
    

Not Recommended

  • myBool == 1
    myInt8 == true
    myInt8 == 1
    myInt8 == int16(1)
    myEnum1.EnumVal == int32(1)
    

himl_0010: MATLAB code with logical operators and functions

ID: Titlehiml_0010: MATLAB code with logical operators and functions
Description

For logical operators and logical functions in MATLAB code, use logical data types

Notes

Logical operators: &&, ||, ~

Logical functions: and, or, not, xor

Rationale
  • Prevent unexpected results

Model Advisor ChecksCheck usage of logical operators and functions in MATLAB Function blocks (Simulink Check)
References
  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’
    IEC 61508-3, Table A.3 (3) 'Language subset’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1(1b) 'Use of language subsets'
    ISO 26262-6, Table 1(1c) 'Enforcement of strong typing'

  • EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'
    EN 50128, Table A.4 (11) 'Language Subset'

  • DO-331, Section MB.6.3.1.g 'Algorithms are accurate'
    DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

Last ChangedR2024a
Examples

Recommended

  • ~myLogical
    (myInt8 > int8(4)) && myLogical
    xor(myLogical1,myLogical2)
    

Not Recommended

  • ~myInt8
    myInt8 && myDouble
    

himl_0012: Usage of MATLAB functions for code generation

ID: Titlehiml_0012: Usage of MATLAB functions for code generation
DescriptionUse only MATLAB functions that support code generation.
RationaleTo detect and avoid the usage of MATLAB functions which are not supported by code generation at earliest possible stages of development.
Model Advisor ChecksCheck MATLAB functions not supported for code generation (Simulink Check)
References
  • IEC 61508-3, Table A.3 (3) 'Language subset’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'

  • EN 50128, Table A.4 (11) 'Language Subset'

    DO-331, Section MB.6.3.1.b 'Accuracy and consistency’ DO-331, Section MB.6.3.2.b 'Accuracy and consistency’

See Alsocoder.screenerFunctions
Last ChangedR2021b

himl_0013: Limitation of built-in MATLAB Function complexity

ID: Titlehiml_0013: Limitation of built-in MATLAB Function complexity
DescriptionWhen authoring MATLAB code, limit the usage of built-in MATLAB functions that may result in generated code that exceeds complexity limits established for your project.
Notes

Complexity limits can vary across projects. Typical limits might be as described in this table:

MetricLimit
Cyclomatic Complexity (Generated Code)40
RationaleImprove testability and maintainability.
Model Advisor ChecksMetrics for generated code complexity (Simulink Check)
References
  • ISO 26262-6, Table 1 (1a) - Enforcement of low complexity

  • ISO 26262-6, Table 1 (1b) - 'Use of language subsets'

  • ISO 26262-6, Table 3 (1b) – ‘Restricted size and complexity of software components’

Last ChangedR2024a