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 Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check MATLAB Code Analyzer messages

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check MATLAB Code Analyzer messages

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check MATLAB Code Analyzer messages

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check MATLAB Code Analyzer messages

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check MATLAB Code Analyzer messages

For check details, see Check 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 (MATLAB)

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 Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check if/elseif/else patterns in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check if/elseif/else patterns in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check if/elseif/else patterns in MATLAB Function blocks

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check if/elseif/else patterns in MATLAB Function blocks

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check if/elseif/else patterns in MATLAB Function blocks

For check details, see Check 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 Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check switch statements in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check switch statements in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check switch statements in MATLAB Function blocks

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check switch statements in MATLAB Function blocks

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check switch statements in MATLAB Function blocks

For check details, see Check 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 Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check usage of relational operators in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check usage of relational operators in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check usage of relational operators in MATLAB Function blocks

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check usage of relational operators in MATLAB Function blocks

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check usage of relational operators in MATLAB Function blocks

For check details, see Check 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’
    IEC 61508-3, Table A.3 (3) 'Language subset’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

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

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

See Also
Last ChangedR2018b
Examples

Recommended

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

Not Recommended

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

himl_0009: MATLAB code with equal / not equal relational operators

ID: Titlehiml_0009: MATLAB code with equal / not equal relational operators
Description

For MATLAB code with equal or not equal relational operators, avoid using the following data types:

  • Single

  • Double

  • Types derived from single or double data types

Note

Consider the following code fragments:

  1. sqrt(2)^2 == 2

  2. sqrt(2^2) == 2

Mathematically, both fragments are true. However, because of floating point rounding effects, the results are:

  1. false

  2. true

Rationale
  • Prevent unexpected results

Model Advisor Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check usage of equality operators in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check usage of equality operators in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check usage of equality operators in MATLAB Function blocks

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check usage of equality operators in MATLAB Function blocks

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check usage of equality operators in MATLAB Function blocks

For check details, see Check usage of equality operators in MATLAB Function blocks (Simulink Check).

References
  • DO-331, Section MB.6.3.1.g 'Algorithms are accurate’
    EN 50128, MB.6.3.2.g ' 'Defensive Programming'

  • 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 (1b) 'Use of language subsets'
    ISO 26262-6, Table 1 (1d) 'Use of defensive implementation techniques'

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

  • MISRA C:2012, Dir 1.1

See Also
Last ChangedR2018b
Examples

Recommended

  • myDouble >= 0.99 && myDouble <= 1.01; % test range

Not Recommended

  • myDouble == 1.0
    mySingle ~= 15.0
    

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 Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check usage of logical operators and functions in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check usage of logical operators and functions in MATLAB Function blocks

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check usage of logical operators and functions in MATLAB Function blocks

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check usage of logical operators and functions in MATLAB Function blocks

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check usage of logical operators and functions in MATLAB Function blocks

For check details, see Check 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(b) 'Use of language subsets'
    ISO 26262-6, Table 1(c) '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 ChangedR2018b
Examples

Recommended

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

Not Recommended

  • ~myInt8
    myInt8 && myDouble