Documentation Center

  • Trial Software
  • Product Updates

MATLAB Functions

himl_0001: Usage of standardized MATLAB function headers

ID: Titlehiml_0001: Usage of standardized MATLAB® function headers
Description

When using MATLAB functions, use a standardized header to provide information about the purpose and use of the function.

Rationale

A standardized header improves the readability and documentation of MATLAB functions. The header should provide a function description and usage information.

See Also
Last ChangedR2014a
ExamplesA typical standardized function header includes:
  • Function name

  • Description

  • Inputs and outputs (if possible, include size and type)

  • Assumptions and limitations

  • Revision history

himl_0002: Strong data typing at MATLAB function boundaries

ID: Titlehiml_0002: Strong data typing at MATLAB function boundaries
Description

To support strong data typing at the interfaces of MATLAB functions, explicitly define the interface for input signals, output signals, and parameters, by setting:

  • Complexity

  • Type

Rationale

Defined interfaces:

  • Allow consistency checking of interfaces.

  • Prevent unintended generation of different functions for different input and output types.

  • Simplify testing of functions by limiting the number of test cases.

Model Advisor Checks
References
  • IEC 61508-3, Table B.9 (5) - Fully defined interface

  • ISO 26262-6, Table 1 (1f) - Use of unambiguous graphical representation

  • EN 50128, Table A.1 (11) - Software Interface Specifications

  • DO-331, Section MB.6.3.2.b - Low-level requirements are accurateand consistent

See Also
Last ChangedR2014a
ExamplesRecommended:

In the Ports and Data Manager, specify the complexity and type of input u1 as follows:

  • Complexity to Off

  • Type to uint16

Not Recommended:

In the Ports and Data Manager, do not specify the complexity and type of input u1 as follows:

  • Complexity to Inherited

  • Type to Inherit: Same as Simulink.

himl_0003: Limitation of MATLAB function complexity

ID: Titlehiml_0003: Limitation of MATLAB function complexity
DescriptionWhen using MATLAB functions, limit the size and complexity of MATLAB code. The size and complexity of MATLAB functions is characterized by:
  • Lines of code

  • Nested function levels

  • Cyclomatic complexity

  • Density of comments (ratio of comment lines to lines of code)

NoteSize and complexity limits can vary across projects. Typical limits might be as described in this table:
MetricLimit
Lines of code60 per MATLAB function
Nested function levels31,2
Cyclomatic complexity15
Density of comments0.2 comment lines per line of code

1Pure Wrappers to external functions are not counted as separate levels.

2Standard MATLAB library functions do not count as separate levels.

Rationale
  • Readability

  • Comprehension

  • Traceability

  • Maintainability

  • Testability

Model Advisor Checks
References
  • IEC 61508-3, Table B.9 (5) - Fully defined interface

  • ISO 26262-6, Table 1 (1f) - Use of unambiguous graphical representation

  • EN 50128, Table A.1(11) - Software Interface Specifications

  • DO-331, Sections MB.6.3.1.e - High-level requirements conform to standards
    DO-331, Sections MB.6.3.2.e - Low-level requirements conform to standards

See Also
Last ChangedR2014a

himl_0005: Usage of global variables in MATLAB functions

ID: Titlehiml_0005: Usage of global variables in MATLAB functions
Description

Avoid using global variables in MATLAB functions. To access shared data, use signal lines or persistent data.

Notes

Using global data in MATLAB code requires the definition of Data Store Memory blocks or Custom Storage class objects. If the read and write access order is not specified correctly, usage of this type of storage can lead to unexpected results.

Rationale
  • Readability

  • Maintainability

  • Deterministic Behavior

Model Advisor Checks
References
  • IEC 61508-3, Table A.3 (3) 'Language subset'

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

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

  • DO-331, Section MB.6.3.3.b 'Consistency'

See Also
Last ChangedR2014a
Examples
  • Recommended

    function [Y,newG] = ...     
      fcn(U,oldG)
      %#codegen
      Y    = oldG * U;
      newG = oldG + 1;
    end
    

  • Recommended

    function Y  = fcn(U)
      %#codegen
      persistent G;
      if isempty(G)
        G = 1;
      end
    

  • Not Recommended

    Write to global data function:

    function fcn(U)
      %#codegen
      global G;
      G = U;
    End
    

    Read from global data function:

    function Y = fcn
      %#codegen
      global G;
      Y = G;
    end
    

Was this topic helpful?