Mab Control Algorithm Modeling Guidelines Using Matlab Simulink and Stateflow v5
Mab Control Algorithm Modeling Guidelines Using Matlab Simulink and Stateflow v5
Version 5.0
1
History
Date Revision
February 2001 Initial document Release, Version 1.00
April 2007 Version 2.00, Update release
July 2011 Version 2.20, Update release
August 2012 Version 3.0, Update release
March 2020 Version 5.0, MAAB guidelines revised and reintroduced as
the MathWorks Advisory Board (MAB) Modeling Guidelines
Trademarks
MATLAB, Simulink, and Stateflow are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be
trademarks or registered trademarks of their respective holders.
2
Table of Contents
1. Introduction ........................................................................................................... 8
Purpose of the guidelines ............................................................................................................. 8
3. Simulink............................................................................................................... 26
Configuration Parameters ........................................................................................................... 26
jc_0011: Optimization parameters for Boolean data types 26
jc_0642: Integer rounding mode setting 26
jc_0806: Detecting incorrect calculation results 27
jc_0021: Model diagnostic settings 28
3
jc_0061: Display of block names 32
db_0140: Display of block parameters 33
jc_0603: Model description 34
jc_0604: Using Block Shadow 35
db_0081: Unconnected signals / blocks 36
db_0032: Signal line connections 37
db_0141: Signal flow in Simulink models 38
jc_0110: Direction of block 41
jc_0171: Clarification of connections between structural subsystems 42
jc_0602: Consistency in model element names 44
jc_0281: Trigger signal names 46
db_0143: Usable block types in model hierarchy 49
db_0144: Use of subsystems 50
jc_0653: Delay block layout in feedback loops 52
hd_0001: Prohibited Simulink sinks 53
Signal ............................................................................................................................................. 54
na_0010: Usage of vector and bus signals 54
jc_0008: Definition of signal names 54
jc_0009: Signal name propagation 55
db_0097: Position of labels for signals and busses 61
na_0008: Display of labels on signals 62
na_0009: Entry versus propagation of signal labels 63
db_0110: Block parameters 64
db_0112: Usage of index 64
jc_0645: Parameter definition for calibration 68
jc_0641: Sample time setting 69
jc_0643: Fixed-point setting 69
jc_0644: Type setting 70
4. Stateflow............................................................................................................ 116
Stateflow blocks/data/events .................................................................................................... 116
db_0122: Stateflow and Simulink interface signals and parameters 116
db_0123: Stateflow port names 117
db_0125: Stateflow local data 118
db_0126: Defining Stateflow events 122
jc_0701: Usable number for first index 124
jc_0712: Execution timing for default transition path 126
jc_0722: Local data definition in parallel states 127
5
jc_0732: Distinction between state names, data names, and event names 192
jc_0730: Unique state name in Stateflow blocks 193
jc_0731: State name format 194
jc_0501: Line breaks in state labels 194
jc_0736: Uniform indentations in Stateflow blocks 195
jc_0739: Describing text inside states 197
jc_0770: Position of transition label 199
jc_0771: Comment position in transition labels 201
jc_0752: Condition action in transition label 204
jc_0774: Comments for through transition 204
6
Verifying adherence to the guidelines 223
Modifying adherence to a guideline 223
7
1. Introduction
Purpose of the guidelines
MathWorks Advisory Board (MAB) guidelines stipulate important basic rules for modeling in Simulink
and Stateflow. The overall purpose of these modeling guidelines is to allow for a simple, common
understanding by modelers and consumers of control system models.
Model runtime errors and recommendations that cannot be implemented are outside of the scope of
these rules.
Guideline template
Guidelines are documented by using a standard template. Use of this template is recommended when
creating original guidelines.
Note: This template specifies the minimum requirements that are needed to understand a guideline.
New items can be added to the template as long as they do not duplicate existing information.
Rule ID
A rule ID, which is used to identify the guideline, consists of two lower case letters and a four-digit
number. The letter and number combination is separated by an underscore. For example, xx_nnnn. A
rule ID is permanent and will not change.
Note: The two-letters in the rule ID identify the guideline author. db, jm, hd, ar are used for Ver 1.0
guidelines. na and jc are used for guidelines created from Ver 2.0 to present.
Sub ID Recommendations
Specifies guideline sub IDs that are recommended for use by the NA-MAAB (North American MathWorks
Automotive Advisory Board) and JMAAB (Japan MathWorks Automotive Advisory Board) modeling
standards organizations. Each organization is a region-specific consortium of OEMs and suppliers; NA-
MAAB represents North America and Europe. JMAAB represents Japan.
MATLAB® Versions
MAB guidelines support all versions of MATLAB and Simulink products. When a rule applies only to a
specific version(s), the version is identified in the MATLAB Version field by using one of these formats:
• All — All versions of MATLAB
• RX, RY, RZ — A specific version of MATLAB
• RX and earlier — Versions of MATLAB until version RX
• RX and later — Versions of MATLAB from version RX to the current version
• RX through RY — Versions of MATLAB between RX and RY
Sub ID
Specifies the condition(s) of the rule. There can be multiple sub IDs per rule ID, which are designated as
either:
• Selectable ― Consist of one lower-case letter (alphabetical order). The choice of whether to
adopt a selectable sub ID is left to the user.
• Mutually Exclusive ― Consist of one lower case letter (alphabetical order) and a single-digit
number. When choosing to accept or reject a mutually exclusive sub ID, only one option can be
selected.
Example
xy_0000 → xy_0000a Selectable (user’s choice)
→ xy_0000b1 Mutually Exclusive (if using, choose from xy_0000b1 or xy_0000b2)
→ xy_0000b2 Mutually Exclusive (if using, choose from xy_0000b1 or xy_0000b2)
Title
The title is unique and provides a brief description of the guidelines.
Description
The description uses figures and tables to provide details for the guideline rules.
Custom Parameters
For rules that include custom parameters, the chosen value is specific for the project with regard to the
item being described.
Example of objects and values are provided in the description field. However, a project’s processes,
condition of the control target, and skill levels of the engineers should be comprehensively evaluated
when specifying a custom parameter.
Rational
The rationale provides reasoning for the use of the guideline with regard to readability, verification
efficiency, efficiency of code after code generation, etc.
See Also
This optional section is only available in guidelines that have additional reference information that may
be helpful to better understand the guideline.
10
2. Naming Conventions
General Conventions
ar_0001: Usable characters for file names
Rule ID: Title ar_0001: Usable characters for file names
Sub ID NA-MAAB: a, b, c, d, e, f, g
Recommendations JMAAB: a, b, c, d, e, f, g
®
MATLAB Version All
Rule
Sub ID Description Custom Parameter
a Only these character types shall be used in file names: File (extension)
single-byte alphanumeric characters (a-z, A-Z, 0-9)
single-byte underscore (_)
11
Rationale
Sub ID Description
Readability is impaired.
abcf
Deviation from the rule can cause unexpected issues.
de Readability is impaired.
If there are multiple files with the same name, the one higher on the path is loaded.
As a result, unnecessary files might be included.
g
Readability is impaired.
Deviation from the rule can cause unexpected issues.
12
【Incorrect】
Rationale
Sub ID Description
Readability is impaired.
abcdef
Deviation from the rule can cause unexpected issues.
13
characters. length
Rationale
Sub ID Description
a Possible that the full path name cannot be display in the user interface.
Content Conventions
jc_0201: Usable characters for subsystem names
Rule ID: Title jc_0201: Usable characters for subsystem names
Sub ID Recommendations NA-MAAB: a, b, c, d, e, f
JMAAB: a, b, c, d, e, f
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Only these character types shall be used in structural -
subsystem names:
Single-byte alphanumeric characters (a-z, A-Z, 0-9)
Single-byte underscore (_)
14
d A structural subsystem name shall not use an -
underscore at the end.
【Incorrect】
Rationale
Sub ID Description
abf Cannot generate code using the configured structural subsystem name.
cde May not be able to generate code using the configured structural subsystem name.
【Incorrect】
Rationale
Sub ID Description
Deviation from the rule can make it difficult to maintain the integrity of the model
ab
and code.
ce Readability is impaired.
Readability is impaired.
d Underscores can be used to separate words. However, they are typically used
as word breaks and can cause misunderstanding in the description.
Readability is impaired.
f
Deviation from the rule can cause unexpected issues.
17
Single-byte spaces are used.
Rationale
Sub ID Description
Deviation from the rule can make it difficult to maintain the integrity of the model
ab
and code.
18
ce Readability is impaired.
Readability is impaired.
d Underscores can be used to separate words. However, they are typically used
as word breaks and can cause misunderstanding in the description.
Readability is impaired.
f
Deviation from the rule can cause unexpected issues.
【Correct】
The hierarchical signal name length
21
(full path bus_all.bus_name_finla.bus_name2.abcdefghijklmn) is less than or
equal to 63 characters.
Rationale
Sub ID Description
a Code generation may not be possible.
23
jc_0792: Unused data
Rule ID: Title jc_0792: Unused data
Sub ID NA-MAAB: a, b
Recommendations JMAAB: a, b
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a The data dictionary (sldd) shall define only the data that Types of data dictionary
is used in the Simulink or Stateflow model.
b The model workspace shall define only the data that is -
used in the Simulink or Stateflow model.
Rationale
Sub ID Description
ab Unused data can affect maintainability and operability.
【Incorrect】
Unused data is defined.
Rationale
Sub ID Description
24
Unused data and events in the Stateflow block can affect maintainability and
reusability.
a
Affects code as a declarative statement concerning unused data is inserted into the
generated code.
25
3. Simulink
Configuration Parameters
jc_0011: Optimization parameters for Boolean data types
Rule ID: Title jc_0011: Optimization parameters for Boolean data types
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Configuration parameter {Implement logic signals as -
Boolean data (vs. double)} shall be selected so that
optimization parameters are activated for logic signals.
Rationale
Sub ID Description
a Using Boolean data can reduce RAM capacity when using C code.
26
【Incorrect】
Configuration parameter {Production hardware signed integer division rounds to} is
set to “Undefined”.
Rationale
Sub ID Description
a Prevents unintended rounding of divided signed integers.
See Also
Sub ID a, see MISRA AC SLSF 008B
27
For R2014b and later, these configuration parameters
shall be set to “Error”:
• {Wrap on overflow}
• {Saturate on overflow}
Rationale
Sub Description
ID
abc Allows detection of operations with invalid values.
See Also
Sub ID a, see hisl_0005c
Diagram appearance
na_0004: Simulink model appearance settings
Rule ID: Title na_0004: Simulink model appearance settings
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
28
a Simulink model appearance settings shall conform with Display option
the project settings.
Example:
Toolbar checked
Viewers checked
Rationale
Sub ID Description
a Standard model appearance improves readability.
See Also
Sub ID a, see MISRA AC SLSF 023A
29
db_0043: Model font and font size
Rule ID: Title db_0043: Model font and font size
Sub ID NA-MAAB: a, b, c, d
Recommendations JMAAB: a, b, c, d
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Block name {font} and {font style} shall conform with the Font
project settings. Font style
Signal name {font} and {font style} shall conform with
the project settings.
b Block name font {size} shall conform with the project Font size
settings.
Signal name font {size} shall conform with the project
settings.
c State labels and box name {font} and {font style} shall Font
conform with the project settings. Font style
Transition labels and comment {font} and {font style}
shall conform with the project settings.
d State labels and box name font {size} shall conform with Font size
the project settings.
Transition labels and comment font {size} shall conform
with the project settings.
Rationale
Sub ID Description
ac Standard fonts improve readability.
bd Standard font size improves readability.
See Also
Sub ID c and d, see MISRA AC SLSF 050B
30
【Incorrect】
The block is too small, so the icon is neither visible nor recognizable.
Rationale
Sub ID Description
When a block is too small, the text and symbol displayed by the icon can be difficult
a
to see, which impairs readability.
【Incorrect】
31
Rationale
Sub ID Description
Consistent placement of the block name improves model readability because it is
a
easier to determine which name corresponds to the block.
32
Rationale
Sub ID Description
a Improves model readability.
See Also
Sub ID a, see MISRA AC SLSF 026A
【Incorrect】
33
Rationale
Sub ID Description
a Readability improves when block parameters are displayed.
See Also
Sub ID a, see MISRA AC SLSF 026E
【Incorrect】
The layer does not include a description.
34
b The format of the layer description shall be consistent in Model description format
the model.
Rationale
Sub ID Description
When a description is not included, the readability of the control specifications is
a
reduced. Usability, maintainability, and portability also decreases.
b Readability is impaired when the description format is not consistent.
See Also
Sub ID a and b, see MISRA AC SLSF 022
【Incorrect】
The block has a drop shadow.
35
Rationale
Sub ID Description
Difficult to determine if a port exists because it is hidden by the shading, which
a
impairs readability.
See Also
Sub ID a, see MISRA AC SLSF 024A
【Incorrect】
36
b The model shall not have subsystems or basic blocks -
that are not connected.
【Correct】
【Incorrect】
Rationale
Sub ID Description
Unconnected lines can have adverse effects, such as simulation errors or failure to
ab
generate code.
37
b Signal lines shall not overlap with other signal lines. -
c Signal lines shall not cross over blocks. -
d Signal lines shall not split into more than two sub lines at -
a single branching point.
【Correct】
【Incorrect】
Exception:
Feedback loops can flow from right to left.
【Correct】
38
【Incorrect】
39
【Incorrect】
【Incorrect】
40
Rationale
Sub ID Description
abc Deviation from the rules can impair readability.
Exception:
When [Delay] is used in a feedback loop, the output can
be to the left.
【Correct】
The block is arranged so that the output is to the right. The output of [Delay] is to the
left.
41
【Incorrect】
The block is arranged so the output is to the left.
Rationale
Sub ID Description
Signal flow can be difficult to understand if the direction of the signals is not
a
consistent.
Exception:
Using [Goto] and [From] to create buses or connect
signals to [Merge].
【Correct】
42
【Incorrect】
【Incorrect】
Signals that are not used in the subsystem are connected to avoid crossing of signal
lines.
43
Rationale
Sub ID Description
a Clarifies structural subsystem connections and execution order.
Eliminating unnecessary connections clarifies the relationship between connections.
B
Deviation from the rule can cause to confusion due to unused input/output signals.
Exception 1:
A signal line that connects to one of the following
44
subsystem types can have a name that differs from that
of the subsystem port label:
Subsystems linked to a library
Reusable subsystems
Exception 2:
When a combination of [Inport], [Outport], and other
blocks have the same name, use a suffix or prefix for the
[Inport] and [Outport] blocks. Any prefix or suffix can be
used for ports, but they must be consistent. For example,
[Inport] uses “in” and [Outport] uses “out”.
Note: [Inport] and [Outport] names and signal names
must have different names.
【Correct】
Names of model elements that connect directly to signal lines are consistent.
【Incorrect】
Names of model elements that connect directly to signal lines are inconsistent.
Rationale
Sub ID Description
Prevent misconnected signal lines.
Readability is impaired.
a
Deviation from the rule can make it difficult to maintain the integrity of the model and
code.
See Also
Sub ID a, see MISRA AC SLSF 036C
45
jc_0281: Trigger signal names
Rule ID: Title jc_0281: Trigger signal names
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a1/a2/a3/a4, b1/b2/b3/b4
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a1 The name of the conditional input block at the destination -
shall include the name of the block at the origin of the
trigger signal
【Correct】
【Incorrect】
【Incorrect】
46
a3 The name of the conditional input block at the destination -
shall include the name of the trigger signal.
【Correct】
【Incorrect】
【Incorrect】
47
【Incorrect】
【Incorrect】
48
【Incorrect】
【Incorrect】
Rationale
Sub ID Description
a1a2a3 Reduces connection mistakes.
a4b1b2 Increases understanding of the relationship between the origin of the trigger signal
b3b4 and the destination.
See Also
Sub ID a1, a2, a3, a4, see MISRA AC SLSF 026C
Block restrictions:
(R2011a and earlier) [Enable] cannot be used at the
root level of the model.
Action ports are not permitted at the root level of a
model.
Layer restrictions:
Data flow layers that are used for basic blocks only.
Other than data flow layers, layers can include blocks that are used for structural
subsystems and all other layers.
Rationale
Sub ID Description
Readability is impaired when subsystems and basic blocks are used in the same
a
layer.
50
the algorithm, or portion thereof, represented in the
diagram.
【Incorrect】
Subsystems are not divided by functional unit.
51
jc_0653: Delay block layout in feedback loops
Rule ID: Title jc_0653: Delay block layout in feedback loops
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Delay] in feedback loops across subsystems shall reside -
in the hierarchy that describes the feedback loop.
【Correct】
[Delay] resides in the hierarchy that describes the feedback loop.
【Incorrect】
[Delay] resides in a subsystem that is nested within the hierarchy which describes
the feedback loop.
52
Rationale
Sub ID Description
Prevents double placement of [Delay].
Clarifying the extent of diversion improves reusability.
a
Improves testability; it is difficult to test a subsystem that contains [Delay] on its own
because past values cannot be entered directly.
53
Signal
na_0010: Usage of vector and bus signals
Rule ID: Title na_0010: Usage of vector and bus signals
Sub ID NA-MAAB: a, b, c, d
Recommendations JMAAB: a, b, c, d
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Mux] and [Demux] blocks shall be used when generating -
and decomposing vectors.
b [Mux] inputs shall be scalars and vectors. -
c [BusCreator] and [BusSelector] shall be used when -
generating and decomposing busses.
d Busses shall connect to blocks that support busses. -
Rationale
Sub ID Description
Prevents issues that are caused by combining vector and bus signals.
abcd
See “Preventing the mixing of busses and Mux” for additional information.
See Also
Sub ID a, b, c, d, see MISRA AC SLSF 015A,B
Important blocks:
An important block is defined by the system input and
output of meaningful results, not by its type.
【Correct】
54
【Incorrect】
Rationale
Sub ID Description
Defining the signal name and displaying the label for the output of meaningful results
a
from important blocks improves the readability of the model.
55
【Incorrect】
{Show propagated signals} is not selected, therefore signal names are not displayed.
Signals that connect to [Bus Creator] and [Outport] do not have names, but {Show
propagated signals} is selected for signals that connect to [Subsystem] and [Outport].
56
Signals that connect to [Bus Creator] and [Outport] have names, but signals that connect
to [Subsystem] and [Outport] also have names.
57
Signals that connect to [Inport] and [Goto] do not have names, therefore {Show
propagated signals} does not need to be selected.
Signals that connect to [Inport] and [Goto] do not have names, therefore signals that
connect to [From] and [Gain] can be left unnamed.
58
【Incorrect】
Signals that connect to [Inport] and [Goto] do not have names, but {Show propagated
signals} is selected for signals that connect to [From] and [Gain].
Signals that connect to [Inport] and [Goto] have names, but signals that connect to [From]
and [Gain] are named.
Signals that connect to [Gain] and [Signal Specification] do not have names, but {Show
propagated signals} is selected for signals that connect to [Signal Specification] and
[Outport].
Signals that connect to [Gain] and [Signal Specification] have names, but signals that
connect to [Signal Specification] and [Outport] have names.
Signals that connect to [Function-Call Generator] and [Function-Call Split] do not have
names, but {Show propagated signals} is selected for signals that connect to [Function-
Call Split] and [Function-Call Subsystem].
59
Regardless of whether signals are propagated, {Show propagated signals} is not
selected.
Signals that connect to [Function-Call Generator] and [Function-Call Split] have names
and signals that connect to [Function-Call Split] and [Function-Call Subsystem] are also
named.
60
Rationale
Sub Description
ID
Prevents signal line connection mistakes.
ab
Prevents signal line name mistakes.
【Incorrect】
The signal line labels and bus labels overlap other labels, signal lines, or blocks.
【Incorrect】
Signal line labels and bus labels are above the signal line.
61
c Signal line labels and bus labels shall be positioned at -
the origin of the connection.
【Correct】
Signal line labels and bus labels are positioned at the origin of the signal line
connection.
【Incorrect】
Signal line labels and bus labels are positioned at the destination of the signal line
connection.
Rationale
Sub ID Description
Adherence to this rule prevents confusion with corresponding names, signal lines,
a
and busses, which improves readability of the model.
Consistent label position prevents confusion with corresponding labels, signal lines,
bc
and busses, which improves the readability of the model.
62
• [Constant] (see exception)
• [Chart]
63
db_0110: Block parameters
Rule ID: Title db_0110: Block parameters
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Block parameters shall not be used to describe: -
Operation expressions
Data type conversion
Selection of rows or columns
MATLAB commands
Rationale
Sub ID Description
Operation expressions, data type conversion, or row or column selection become a
magic number in generated code, which makes consistency between the model and
code difficult to maintain. Adjusting parameters also becomes difficult.
a
Describing the calculation formula within the block decreases readability.
The result of executing a MATLAB command is reflected in the code, which makes
consistency between the model and code difficult to maintain.
64
【Incorrect】
A uniform index mode is not used.
65
a2 A vector signal shall use a 1-based index mode. -
【Correct】
A uniform 1-based index mode is used.
66
【Incorrect】
A uniform index mode is not used. (Same as db_0112, Sub ID_a1).
67
Rationale
Sub ID Description
a1a2 Logic is easier to understand when using a uniform index mode.
68
【Incorrect】
Rationale
Sub ID Description
A literal constant in the model will propagate as a literal constant in the generated
a
code, making calibration impossible.
Exceptions include:
[Inport]
[Outport]
Atomic subsystem
Blocks with state variables, such as [Unit Delay] and
[Memory]
Signal conversion blocks, such as [Data Type
Conversion] and [Rate Transition]
Blocks that do not have external inputs, such as
[Constant]
[Chart]
Rationale
Sub ID Description
Discrepancies can occur in the processing of the model because of different
simulation times.
a
Maintainability of the model deteriorates when a specific sample time is set for each
block individually.
69
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a When block parameters {Data type} is a fixed-point (fixdt) -
setting and {Scaling} is “Slope and bias”, parameter
{Bias} shall be set to “0”.
Rationale
Sub ID Description
When the bias in a model is not uniform:
Behavior of the model is impossible to determine by its appearance.
a
Unintended overflows and underflows occur.
Results in wasteful operation and deterioration of code efficiency/computing load.
70
Rationale
Sub ID Description
When the data type is set in a block and it differs from the type setting in the data
object, it can be difficult to determine which setting is correct. This can impair
readability.
When the type is set in the block,
—maintainability is affected when the signal line type changes.
Exceptions:
Inside a reusable function
When all block structures are identical, differences between input/output data type
can result in different C source code that is not reusable. For reusable functions,
data types of input/output blocks should be specified at the subsystem level.
71
Rule
Sub ID Description Custom Parameter
a Conditional input blocks shall be positioned at the top of -
the subsystem.
【Correct】
【Incorrect】
72
Rule
Sub Description Custom Parameter
ID
a When both conditions are met for a conditional -
subsystem:
- Includes a block with initial conditions (i.e.
[Constant] and [Delay])
- Connects to [Outport]
The initial condition shall be defined on [Outport].
【Incorrect】
73
The initial condition is undefined.
Rationale
Sub Description
ID
a The model may not behave as intended when the initial condition is unclear.
74
a Only conditional subsystem output signals shall input to -
[Merge].
【Correct】
Conditional subsystem output signal is input to [Merge].
【Incorrect】
0
Rationale
Sub ID Description
a Prevents the simulation from proceeding as intended.
75
The {If expression} only defines the input variables.
【Incorrect】
The {If expression} defines a comparison operation.
Rationale
Sub ID Description
Visual comprehension of control conditions is easier when logical operations are
described outside of [If].
a
Describing logical operations outside of [If] allows verification to focus on the logical
operation.
Rationale
Sub ID Description
Determining whether there is pointless processing or if something is missing from the
a design (such as a missing description) is easier when the processing of exceptions
(else, default) is explicitly set in the model .
jc_0657: Retention of output value based on conditional control flow blocks and Merge
blocks
Rule ID: Title jc_0657: Retention of output value based on Conditional Control
Flow blocks and Merge blocks
Sub ID NA-MAAB: a2
Recommendations JMAAB: a1/a2
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a1 Unused action ports shall connect to [Terminator] when -
these conditions are met:
- Past value is retained
- [Merge] and a conditional flow block, such as [If]
or [Switch Case], are used to switch functions.
77
【Correct】
[If] example
【Incorrect】
[If] example
78
[Switch Case] example
79
[Switch Case] example
【Incorrect】
[If] example
80
[Switch Case] example
Rationale
Sub ID Description
Improves code efficiency.
a1 Connections to [Terminator] can be used when past values are held other than by
the default (else).
a2 Retaining past values is explicit.
Operation blocks
na_0002: Appropriate usage of basic logical and numerical operations
Rule ID: Title na_0002: Appropriate usage of basic logical and numerical
operations
Sub ID NA-MAAB: a, b
Recommendations JMAAB: a, b
MATLAB® Version All
Rule
81
Sub ID Description Custom Parameter
a Logical signals shall not connect to blocks that operate Blocks receiving
on numerical signals. numerical signals
【Correct】
Numerical values are compared to determine if they are equal.
【Incorrect】
A logical output is connected directly to the input of blocks that process numerical
inputs.
b Numerical signals shall not connect to blocks that Blocks receiving logical
operate on logical signals. signals
【Correct】
82
Logical signal is inverted by using a logical operation.
【Incorrect】
A block that is used to perform logical operations is being used to perform numerical
operations.
A numerical output is connected to the input of blocks that process logical inputs.
A block that is used to perform numerical operations is being used to perform logical
operations.
Inputs other than logical values can be provided to the block. However, [Enable
Port] can receive only logical signals that have On/Off.
83
[Product] performs logical operations when it connects the numerical operations
result to a block that receives the logical value [Enable Port].
Rationale
Sub ID Description
When numerical and logical values are treated the same, the original intention
ab becomes unclear and the next operation in the model can be incorrectly interpreted,
further compounding the error.
The second input to the add and subtraction [Sum] block is a feedback loop, so the
{icon shape} is “round”.
84
【Incorrect】
This is not a feedback loop, but the {icon shape} of the add and subtraction [Sum]
block is “round”.
b The “+” mark shall be used for the first input to the add -
and subtraction [Sum] block.
For a feedback loop, the first input can be set by using
the “-” mark.
【Correct】
The “+” mark is used for the first input to the add and subtraction [Sum] block.
The second input to the add and subtraction [Sum] block is a feedback loop, so the “-”
mark is used.
【Incorrect】
The sign for the first input to the add and subtraction [Sum] block is the “-” mark.
85
c The add and subtraction [Sum] block shall not have more -
than two inputs.
【Correct】
The add and subtraction [Sum] block has no more than two inputs.
【Incorrect】
The add and subtraction [Sum] block has three inputs.
Rationale
Sub ID Description
a Adherence to the guideline improves readability of the model.
Readability of the control specification improves when the sign for the first input is
b
consistent.
c The order of operations is clearly defined.
86
Rule
Sub ID Description Custom Parameter
a The “*” mark shall be used for the first input to a -
multiplication and division [Product] block.
【Correct】
The “*” mark is used for the first input to the multiplication and division [Product]
block.
【Incorrect】
The “/” mark is used for the first input to the multiplication and division [Product]
block.
【Incorrect】
The block has three inputs.
87
Rationale
Sub ID Description
When checking the block, the input order of the expression and block is reversed,
which impairs readability.
a For floating point numbers, the code is generated according to the operation order in
the block ((1÷1st input)) × 2nd input). However, if division is performed later, the
number of operations can be reduced.
b The order of operations is clearly defined.
【Incorrect】
【Incorrect】
89
【Incorrect】
Simulation result
【Incorrect】
90
【Incorrect】
【Incorrect】
91
Simulation result: Plot as Y = |Z|
【Incorrect】
Simulation result
92
【Incorrect】
【Incorrect】
【Incorrect】
93
h When using [Math Function] and block parameter {Function} is -
set to “reciprocal”, the input to the block shall not be zero.
【Correct】
Replace within ±eps with ±eps
Simulation result: Simulation results is not inf, but since it is close to zero, the change
in the output value is significant.
【Incorrect】
94
【Incorrect】
【Incorrect】
Rationale
Sub ID Description
a1c1de The result of entering an invalid value is implementation dependent. Deviation from
f1ghij the rules can result in unintended behavior.
a2 Correct settings prevent unintended behavior that can result from using invalid values.
The block can become optimized out of the generated code, resulting in a block that
b
you cannot trace to the generated code.
Correct settings prevent unintended behavior that can result from using negative
c2f2
values.
95
jc_0622: Usage of Fcn blocks
Rule ID: Title jc_0622: Usage of Fcn blocks
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a When [Fcn] has operators with different priorities, -
parentheses shall be used to specify the priority order.
Rationale
Sub ID Description
When operators have different priorities and the computation order is not clearly
a specified by using parentheses, readability is impaired and can be misinterpreted.
This can result in unintended behavior.
【Incorrect】
Some of the icon shapes for [Logical Operator] are not “rectangular”.
96
Rationale
Sub ID Description
When describing the same function, using a consistent expression improves
a readability. Since “Characteristics” shapes are similar, the risk of misinterpretation is
greater than with “rectangular” shapes.
【Incorrect】
Rationale
Sub ID Description
97
Using constant values and the same comparison method reduces misinterpretation of
a
the model.
【Incorrect】
Uses floating-point type equivalence comparison operations (==, ~=).
Rationale
Sub ID Description
Due to the characteristics of the floating-point, since the error is included in the value,
a the result of the equivalence comparison operation may be false when it was
expected to be true.
【Incorrect】
[Memory] is used in the discrete type model or subsystem.
99
Rationale
Sub ID Description
a Adherence to the rule improves readability of the model.
【Incorrect】
[Tapped Delay] is not used.
100
b When holding past values, [Delay] shall be used to -
obtain the oldest value only.
【Correct】
[Delay] is used.
【Incorrect】
[Delay] is not used.
Rationale
Sub Description
ID
[Tapped Delay] is set with arrays that hold past values, which improves code
a
readability to assist code efficiency.
B Improves model readability and code efficiency.
101
a [Discrete-Time Integrator] block parameters {Upper -
saturation limit} and {Lower saturation limit} shall be
defined.
【Correct】
Block parameters {Upper saturation limit} and {Lower saturation limit} are defined.
【Incorrect】
Block parameters {Upper saturation limit} and {Lower saturation limit} are not
defined.
Jc_0627
b When [Discrete-Time Integrator] block parameters -
{Upper saturation limit} and {Lower saturation limit} are
defined as Simulink.Parameter, parameter {Data
type} shall be set to “auto”.
【Correct】
{Data type} is set to “auto”.
102
【Incorrect】
{Data type} is not set to “auto”.
Rationale
Sub ID Description
Avoids block output overflow and prevents other computation blocks that use the
a
output of this block from producing unexpected results.
Simulation errors occur when {Data type} is set to a value other than “auto”, “single”,
B
or “double”.
103
jc_0628: Usage of Saturation blocks
Rule ID: Title jc_0628: Usage of Saturation blocks
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Saturation] and [Saturation Dynamic] shall be used to -
limit physical quantity.
Type conversion shall not be used.
The upper and lower limits for the data type maximum
and minimum values shall not be set.
【Correct】
[Saturation Dynamic] is used to limit physical quantity. Type conversion is not being
used.
【Incorrect】
[Saturation Dynamic] is not being used to limit physical quantity. Type conversion is
being used. The upper and lower limits for the data type maximum and minimum
values are set.
Rationale
Sub ID Description
a Consistent use of [Saturation] improves maintainability of the model.
104
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Data Type Conversion] shall be used when changing -
the data type of the block output signal.
【Correct】
[Data Type Conversion] is used to convert the data type of the [Divide] output signal.
【Incorrect】
[Data Type Conversion] is not used to convert the data type of the [Divide] output
signal.
Rationale
Sub ID Description
Dividing the math operations and type cast can help to clarify the order of execution
a
and data type for each expression.
Other blocks
db_0042: Usage of Inport and Outport blocks
Rule ID: Title db_0042: Usage of Inport and Outport blocks
Sub ID NA-MAAB: a, b
Recommendations JMAAB: a, b, c
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Inport] shall be positioned on the left side of the diagram, -
but can be moved to prevent the crossing of signals.
【Correct】
[Inport] is positioned on the left side of the diagram.
105
【Incorrect】
[Inport] is not positioned on the left side of the diagram.
106
【Incorrect】
[Outport] is not positioned on the right side of the diagram.
【Incorrect】
[Inport] is duplicated.
107
Rationale
Sub ID Description
abc Defined operation rules improve readability.
【Incorrect】
The {icon display} for [Inport] and [Outport] is not "Port number".
108
Rationale
Sub ID Description
Improves readability by displaying the port number of [Inport] and [Outport].
Allows for easy identification of port numbers that are within a subsystem.
a Prevents misconnections to hierarchized subsystems by displaying the block names
and making the names of signal lines to the [Inport] or [Outport] the same as the
block names.
110
【Incorrect】
The data port and output port have different data types.
Rationale
Sub ID Description
a Prevents implicit data conversion.
111
{Diagnostic for default case} to “None”.
【Correct】
【Incorrect】
Rationale
Sub ID Description
Unintended output can occur when there is only one data port because the block
a
changes to extract scalars from vectors.
The control port is an input range that expects an integer value of zero or greater.
When a signed or non-integer signal is connected to the control port, it can appear
b as a misconnection.
There is a possibility of data ports being unintentionally selected when negative or
non-integer values are input.
When block parameter {Data port order} is set to “Specify indices”, any value that is
input to [Multiport Switch], other than the index specified for the control port, is
c
treated the same as the last value of the specified index. As a result, an unintended
data port can be selected.
112
na_0020: Number of inputs to variant subsystems
Rule ID: Title na_0020: Number of inputs to variant subsystems
Sub ID NA-MAAB: a
Recommendations JMAAB: a, b
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a The number of inputs/outputs of a [Variant Subsystem] -
and its child subsystem or [Model Reference] shall be the
same.
【Correct】
The number of inputs to the child subsystem is the same.
【Incorrect】
The number of inputs to the child subsystem is different.
Rationale
Sub ID Description
Unconnected signals can be unintentionally overlooked when the number of
ab
inputs/outputs is different.
【Correct】
FUNC is a logical type.
【Incorrect】
An active subsystem will not exist when FUNC is not 1 or 2.
【Incorrect】
An active subsystem will not exist when FUNC is not 1 or 2.
Rationale
Sub Description
ID
Prevents the omission of conditions.
ab
There may not be an active subsystem when conditions are omitted.
115
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Variant conditions shall be used to prohibit compound -
conditions that are formed from multiple variables.
Exception:
When using default variants, conditional expressions
that are formed from multiple variables can be used.
【Correct】
The variant condition is set by a single condition that is formed from multiple
variables.
【Incorrect】
The variant condition is set by a compound condition that is formed from multiple
variables.
Rationale
Sub ID Description
Complicates the conditions, which makes it difficult to determine which subsystem
will become active. This can result in conditions being omitted.
a
When conditions are omitted, there is a risk that there may not be an active
subsystem.
4. Stateflow
Stateflow blocks/data/events
db_0122: Stateflow and Simulink interface signals and parameters
Rule ID: Title db_0122: Stateflow and Simulink interface signals and
parameters
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Chart] parameter {Use Strong Data Typing with Simulink -
I/O} shall be selected so that strong data typing between
116
a Stateflow chart and Simulink is permitted.
Note: {Use Strong Data Typing with Simulink I/O} is
available only when [Chart] property {Action Language} is
set to “C”.
【Correct】
Parameter {Use Strong Data Typing with Simulink I/O} is selected, so the input and
output are set to “uint8” type.
【Incorrect】
Parameter {Use Strong Data Typing with Simulink I/O} is not selected, so the input
and output are set to “double” type.
Rationale
Sub ID Description
When {Use Strong Data Typing with Simulink I/O} is not selected, the Simulink
signal data type that can input and output to [Chart] is set to “double” type. As a
result, type conversion is required prior to input and after output, which increases the
number of blocks and decreases readability.
When {Use Strong Data Typing with Simulink I/O} is not selected, the Simulink
a signal data type that can input and output to [Chart] is set to “double” type. However,
input data of any type in [Chart] can connect directly with that signal.
When these two signals have different data types, an implicit data type conversion
occurs. By selecting {Use Strong Data Typing with Simulink I/O}, the implicit data
type conversion does not take place and a data type inconsistency error is
generated. This prevents misunderstandings due to differences in data type, thus
improving readability.
117
Exception: Reusable Stateflow blocks can have different
port names.
Rationale
Sub ID Description
Improves readability.
a
Code generation may not be possible.
【Incorrect】
Scope has set “Local” local data at the machine level.
118
b Scope shall not define “Constant” local data at the -
machine level.
【Correct】
【Incorrect】
Scope has set “Constant” local data at the machine level.
119
c Scope shall not define “Parameter” local data at the -
machine level.
【Correct】
【Incorrect】
Scope has set “Parameter” local data at the machine level.
120
d A Stateflow block with parent-child relationships shall not -
include local data with the same name.
【Correct】
【Incorrect】
A Stateflow block with parent-child relationships has local data with the same name.
121
Rationale
Sub ID Description
When local data is defined at the machine level, it is shared with all blocks in the
a model. The data will not behave like a local variable and can be influenced by any
operation.
Adherence to the rules prevent the definition from disappearing when copying a
abc
Stateflow block to another model.
When a Stateflow block with parent-child relationships includes local data with the
d same name, readability decreases due to lack of clarity with regard to the influence
of the local data.
122
a Stateflow events shall be defined by the smallest scope -
level in the Stateflow block being used.
【Correct】
【Incorrect】
Rationale
123
Sub ID Description
a Limiting use locations increases reliability.
【Incorrect】
{First index} is set to a combination of “0”, “1”, and “2”.
124
a2 When [Chart] property {Action Language} is set to “C”, -
Stateflow data property {First index} shall be set to “1”.
【Correct】
The {First index} is set to “1”.
125
【Incorrect】
The {First index} is set to a combination of “0”, “1”, and “2”.
Rationale
Sub ID Description
a1 Logic becomes easier to understand when {First index} is uniform.
Logic becomes easier to understand when {First index} is uniform. However, C
a2 language is 0-based, which decreases the readability of the code as the index
calculation process is 1-based. This is reflected in the generated code.
【Incorrect】
Local variables are not defined in the state being used.
127
Rationale
Sub ID Description
Readability and maintainability can be improved by explicitly limiting the valid range
a
of the variables, thereby avoiding unintended references and changes.
Stateflow diagram
jc_0797: Unconnected transitions / states / connective junctions
Rule ID: Title jc_0797: Unconnected transitions / states / connective junctions
Sub ID NA-MAAB: a, b
Recommendations JMAAB: a, b
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Chart] shall not have unconnected transitions. -
【Correct】
[Chart] does not have unconnected transitions.
128
【Incorrect】
[Chart] has unconnected transitions.
129
【Incorrect】
[Chart] has unconnected exclusive (OR) states and connective junctions without a
transition source.
Rationale
Sub ID Description
Unconnected transitions can result in adverse effects, such as misinterpretation of
ab
simulation results or failure to generate code.
Rationale
Sub ID Description
Redundant descriptions impair readability.
a
Generated code includes unnecessary state variables.
130
jc_0721: Usage of parallel states
Rule ID: Title jc_0721: Usage of parallel states
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Substates of parallel states shall not be parallel states. -
【Correct】
【Incorrect】
Substates of parallel states are parallel states.
131
Rationale
Sub ID Description
Behavior is not affected by nesting parallel states in a parent superstate.
a
Hierarchization of the parallel state decreases readability.
【Incorrect】
Transition lines cross.
132
b Transition lines shall not overlap other transition lines. -
【Correct】
Transition lines do not overlap other transition lines.
【Incorrect】
Transition lines overlap.
【Incorrect】
Transition lines cross over states.
133
【Incorrect】
Transition lines are not drawn vertically or horizontally.
【Incorrect】
Unnecessary connective junctions are used.
134
Rationale
Sub ID Description
a Difficult to understand the relationship between states when transition lines cross.
b Difficult to understand the relationship between states when transition lines overlap.
Difficult to understand the relationship between states when transition lines cross
c
over states.
d Consistent application of transition lines improves readability.
Transitions can be difficult to understand when unnecessary connective junctions
e
are used.
135
【Incorrect】
The default transition line is not connected.
【Incorrect】
A default transition line is connected for parallel state AA.
136
【Incorrect】
Multiple default transitions are included in the same level of state A.
137
【Incorrect】
The default transition of state A is not connected vertically to the upper part of the
state.
138
【Incorrect】
The default transition of state AB is not positioned to the top left in the same level.
139
【Incorrect】
The default transition extends beyond the boundaries of the state.
140
【Incorrect】
The path with the lowest priority in the transition path for the default transition is not
an unconditional transition.
Rationale
Sub ID Description
Simulation errors can occur when a state chart does not include default transition
lines.
a
When default transitions are included in a flow chart, it is impossible to determine
whether this is intentional or through failure to insert them.
b Readability improves when there are no unnecessary default transitions.
The state may not function as intended and produce a warning when multiple default
c
transitions are included in the same level.
Readability decreases when there are curves or variations in the angle or position of
d
default transitions.
Readability decreases when there are variations in the position of the transition
e destination state or transition destination connective junction for the default
transition.
141
Readability decreases when a default transition extends beyond the boundary of a
f
state and intersects with state boundaries and expressions.
When there is not an unconditional transition in the transition path of the default
g transition, the transition destination disappears if all conditions of the transition path
are not met. This can result in unintended behavior.
142
【Incorrect】
Direct transition from an external state to a child state in a different state.
Direct transition from an external child state to a child state in a different state.
143
Rationale
Sub ID Description
Direct transitions between child states can complicate the states and decrease
a
readability.
【Incorrect】
Connective junctions are used to separate complex conditions.
144
Rationale
Sub ID Description
Deviation from the rule can cause backtracking, which results in unintended
a
behavior.
145
【Incorrect】
The internal transition line does not begin at the left edge of the state.
146
Rationale
Sub ID Description
a Adherence to the rule improves readability.
147
【Incorrect】
148
a2 When multiple internal transitions are used in a single -
state, they shall be listed from top to bottom in the order
of execution.
【Correct】
149
【Incorrect】
Rationale
Sub ID Description
The number of transition conditions is unclear when multiple internal transitions are
a1 used. By limiting the use of internal transitions to a single use, transitions are clearer
and readability improves.
Using multiple internal transitions can prevent transition lines from crossing and
a2 simplifies state transitions.
Arranging internal transitions in execution order improves readability.
150
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a A state shall not include state actions (entry, during, or -
exit) and flow charts.
【Correct】
Within the state, only one of either a State action or a flow chart is described.
【Incorrect】
The state has both a state action and a flow chart.
151
Rationale
Sub ID Description
a The execution order becomes difficult to understand, which decreases readability.
152
【Incorrect】
Transition actions are used.
Exception:
Diagonal transition lines in loop constructs.
【Correct】
The condition is positioned on a horizontal transition line and the condition action is
on a vertical transition line.
153
【Incorrect】
The condition is positioned on a vertical transition line and the condition action is on
a horizontal transition line.
Rationale
Sub ID Description
a The transition action in a flow chart is not executed.
b Consistent positioning of conditions and condition actions improves readability.
154
a When a transition line with a transition condition -
originates from a connective junction, t unconditional
transition line shall also begin from that junction.
【Correct】
【Incorrect】
0762
155
【Incorrect】
The {execution order} for the unconditional transition line is not the last value.
Rationale
Sub ID Description
Prevents unintended behavior that results from backtracking.
a Setting an unconditional transition explicitly defines the behavior for when the
condition is not met.
Setting the unconditional transition to take precedence can prevent unintended
b
behavior.
156
jc_0775: Terminating junctions in flow charts
Rule ID: Title jc_0775: Terminating junctions in flow charts
Sub ID NA-MAAB: a1/a2
Recommendations JMAAB: a1/a2
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a1 Only one terminating junction shall be used. -
【Correct】
【Incorrect】
There is more than one terminating junction.
157
【Incorrect】
There is more than one terminating junction and input.
Rationale
Sub ID Description
One terminating junction improves understanding of the logic end point.
a1a2
Using a consistent style for terminating junction improves readability.
158
【Incorrect】
【Incorrect】
Rationale
Sub ID Description
a The compiler can misinterpret the comments as a program.
A line break in the middle of a comment makes it difficult to determine whether
the part being edited is in the comment. There is also a possibility that the
b comment is nested.
When [Chart] property {Action Language} is set to “MATLAB”, comments must
use %.
159
Conditional transition / Action
jc_0790: Action language of Chart block
Rule ID: Title jc_0790: Action language of Chart block
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Chart] property {Action Language} shall be set to “C”. -
【Correct】
{Action Language} is set to “C”.
【Incorrect】
{Action Language} is set to “MATLAB”.
Rationale
Sub ID Description
160
Using a consistent action language improves readability because there is not a
difference in syntax.
Easier to maintain consistency between the model and the generated code when
a
using C as the action language as compared to MATLAB.
Easier to understand the model for users who are familiar with the C programming
language.
Exceptions:
Initial value is 0
Increment, decrement 1
【Correct】
Numeric literals are not used.
【Incorrect】
0711
Rationale
Sub ID Description
Only the modeler will understand the purpose of the value when numeric literals are
used to write constants, which decreases readability.
a
Constants that are intended for calibration are generated in the code using numeric
literals.
161
jm_0011: Pointers in Stateflow
Rule ID: Title jm_0011: Pointers in Stateflow
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a [Stateflow] shall not use pointer variables. -
【Correct】
Pointer variables are not used.
【Incorrect】
Pointer variables are used.
162
Rationale
Sub ID Description
Readability is impaired when pointer variables are used.
a
Code generation may not be possible.
163
【Incorrect】
Variables k and kk have multiple meanings (usages) in a single [Chart].
164
Rationale
Sub ID Description
Variables can be misinterpreted when the variable name is different than the
a
meaning of the numerical value that is assigned to the variable.
165
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a1 Stateflow events shall be used only in [Stateflow] output. -
【Correct】
Event is used only in the [Stateflow] output.
【Incorrect】
Event is used other than in the [Stateflow] output.
166
【Incorrect】
The state that receives the broadcast has not been defined in the send syntax.
Rationale
Sub ID Description
Recursive processing in a chart is prevented by using Stateflow events in [Stateflow]
a1
output only.
168
Improves readability because transitions that are triggered by events are clearly
a2a3
identified.
169
Rationale
Sub ID Description
ab Consistent modelling improves readability and maintainability.
170
Rationale
Sub ID Description
The execution order will differ depending on the order in which they are described.
a Execution order can be difficult to understand when the action type is described
multiple times.
【Incorrect】
This example illustrates how the model behavior in [Chart] is misinterpreted. It
appears that TBD is output when state action type exit(ex) is used, but it is in fact
being overwritten by the state action type entry of the transition destination state. It is
not outputted by [Chart].
171
Rationale
Sub ID Description
Execution timing can be difficult to understand when state action type exit(ex) is
a used in combination with a conditional action, a transition action, or state action type
entry(en). This can result in misinterpretation of the model behavior.
【Incorrect】
The update is performed by using “during”.
172
Rationale
Sub ID Description
The execution order of the transition condition and implement of “during” can be
a
difficult to understand, which increases the risk of errors.
173
【Incorrect】
Execution order 1 is an unconditional transition and conditional expression [C1] is
described in execution condition 2.
【Correct】
Includes a state transition.
174
【Incorrect】
Includes state transition. The unconditional transition line is higher in the execution
order than the conditional transition line.
Rationale
Sub ID Description
An unconditional transition that is in any position other than the last in the execution
a order causes the subsequent transition to be a dead path, which results in
unintended simulation behavior.
175
Sub ID Description Custom Parameter
a1 Transition actions shall not be used in a state chart. -
【Correct】
Only a condition action is used in the state chart.
【Incorrect】
A transition action is used in the state chart.
176
【Incorrect】
[Chart] 0774
Rationale
Sub ID Description
a1 Prevents confusion with a condition action, thus improving readability.
A condition action executes upon entering a transition. A transition action executes
a2 after determining whether it can transition to the next state. Adherence to the rule
prevents confusion between a conditional action and a transition action.
177
a1 Variables, constants, or parameters in a Stateflow block -
shall not be used to perform division operations.
【Correct】
Division is performed outside of [Chart].
【Incorrect】
Division occurs within [Chart].
178
a2 When division occurs in a Stateflow block, the process shall -
prevent division by zero.
【Correct】
The process is defined to prevent division by zero.
【Incorrect】
The process does not prevent division by zero.
179
Rationale
Sub ID Description
Deviation from the rule can cause unintended operation and code generation
a1a2
results.
180
【Incorrect】
A MATLAB command is used in Stateflow blocks.
181
【Incorrect】
[MATLAB Function] is not used for a MATLAB command.
Rationale
Sub ID Description
Not all MATLAB commands are supported for code generation. As a result, code
a1
may not be generated for these unsupported MATLAB commands.
Not all MATLAB commands are supported for code generation. As a result, code
may not be generated for these unsupported MATLAB commands.
a2
Readability improves when C and MATLAB action languages are described
separately.
jc_0481: Use of hard equality comparisons for floating point numbers in Stateflow
Rule ID: Title jc_0481: Use of hard equality comparisons for floating point
numbers in Stateflow
182
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a These equality comparison operators shall not be used -
in floating-point operands:
• “==”
• “!=”
• “~=”
【Correct】
Equality comparison operators are not used in floating-point operands.
【Incorrect】
Equality comparison operator “==” is used in floating-point operands.
Rationale
Sub ID Description
Due to the nature of the floating-point data type, as it contains an error, the result of
a the equivalence comparison operation may be false when it was expected to be
true.
183
a When [Chart] property {Action Language} is set to “C”, -
operators (“&”, “|”, “^”, “~”) shall be used only for bit
operations.
【Correct】
Operators (“&”, “|”, “^”, “~”) are used for bit operations.
【Incorrect】
Operators “&”, “|”, “^”, “~” are not used for bit operations.
184
b3 When [Chart] property {Action Language} is set to “C”, -
operator “<>” shall be used for inequality operations.
【Correct】
Operator “<>” is used for inequality operations.
“!” is used
【Incorrect】
An operator other than “!” should be used for logical negation.
185
“!” other than “!” should be used.
Rationale
Sub ID Description
When either of these [Chart] properties are set as follows:
• {Action Language} is set to “MATLAB”
• {Action Language} is set to “C” and {Enable C-bit operations} is selected,
a
“&&” and “&”, “||” and “|”, have the same calculation function. However, when “&&”
and “&” or “||” and “|” are combined in the same chart, it can be difficult to determine
whether these are separate calculation functions or the same calculation function.
186
【Incorrect】
Logical constants are compared to each other.
Rationale
Sub ID Description
Readability improves with consistent use of “boolean-valued signal==true(boolean
type constant)” or “(boolean-valued signal)” for logical signal condition expressions.
a
Prevents redundancy in the model.
Deviation from the rule can cause unexpected issues.
【Incorrect】
Negative values cannot be input into 16-bit environments.
(Negative values can be input into 32-bit environments)
Rationale
Sub ID Description
As the results are depend on the execution environment, unintended results can
a
occur.
188
Variables have different data types but are explicitly typecast before calculation.
Example: Comparison operation
The data type of actual arguments and formal arguments in the function call are the
same.
【Incorrect】
Variables use different data types for calculations.
Example: Comparison operation
189
Calculations are performed between unsigned integer type variables and signed
integers.
The data type of actual arguments and formal arguments in the function call are
different.
Rationale
Sub ID Description
a Implicit data type conversion can produce unexpected results.
【Incorrect】
190
a2 The abs library function shall not be used. -
b1 A negative number shall not be entered when using the -
sqrt library function.
【Correct】
【Incorrect】
【Incorrect】
191
【Correct】
【Incorrect】
Label description
jc_0732: Distinction between state names, data names, and event names
Rule ID: Title jc_0732: Distinction between state names, data names, and event
names
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a An identical name shall not be used for state, data (inputs -
and outputs, local data, constants, parameters, data
store memory), or event names in a single [Chart].
【Correct】
Names are not duplicated.
192
【Incorrect】
Names are duplicated.
Rationale
Sub ID Description
a Using unique names prevent misunderstanding.
Rationale
Sub ID Description
Readability is impaired.
a
Deviation from the rule can cause unintended code behavior.
193
jc_0731: State name format
Rule ID: Title jc_0731: State name format
Sub ID NA-MAAB: a
Recommendations JMAAB: a
®
MATLAB Version All
Rule
Sub ID Description Custom Parameter
a The state name shall be followed by a new line that -
does not include a slash (/).
【Correct】
【Incorrect】
Rationale
Sub ID Description
a Readability improves when state names are described consistently.
194
【Incorrect】
Rationale
Sub ID Description
a Readability is impaired.
195
【Incorrect】
Executable statements do not have a single-byte space at the start of the line.
【Incorrect】
A blank space is entered before the “[“ and “{“ of the transition label condition,
condition action, and transition action.
196
c At least one single-byte space shall be entered after Number of single-byte
the “/” of a transition action. spaces
【Correct】
Single-byte spaces are entered after the “/” of the transition action.
【Incorrect】
There are no single-byte spaces after the “/” of the transition action.
Rationale
Sub ID Description
Using uniform indents before the executable statement clarifies the link between
a the state action type of a state label and the execution statement, improving
readability.
Using uniform indents for transition conditions, condition actions, and transition
b
actions improves readability.
c Consistent use of blank spaces improves readability.
197
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Text inside a state shall not extend beyond the -
boundaries of the state.
【Incorrect】
198
Rationale
Sub ID Description
When the text inside a state extends beyond its boundaries, it can be difficult to
a
determine which state the text belongs.
199
【Incorrect】
The positioning of transition labels is inconsistent and do not correspond to the
transition line.
【Incorrect】
The positioning of transition labels is inconsistent and do not correspond to the
transition line.
200
Rationale
Sub ID Description
Consistent positioning of transition labels makes the correspondence between
a1a2
label and line easier to understand.
201
【Incorrect】
The position of the comments in the transition labels is inconsistent.
202
【Incorrect】
The position of the comments in the transition labels is inconsistent.
Rationale
Sub ID Description
Uniform positioning of comments in transition labels clarifies to which transition
a1a2 condition, condition action, transition action, or Stateflow event the label
corresponds.
203
jc_0752: Condition action in transition label
Rule ID: Title jc_0752: Condition action in transition label
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Parentheses in condition actions shall use only curly -
brackets on a single line.
(A new line shall start before and after curly brackets.)
【Correct】
Note: The example is for a flow chart, but the rule also applies to state transitions.
Incorrect
Rationale
Sub ID Description
a Clarifying condition actions improves readability.
204
A clarifying comment is provided.
【Incorrect】
A clarifying comment is not provided on the condition path, so it is difficult to
determine whether the lack of action is intentional.
Rationale
Sub ID Description
Clarifies that the processing is deliberately excluded.
a The comment that is added to a transition label is also included in the generated
code.
Miscellaneous
jc_0511: Return values from a graphical function
Rule ID: Title jc_0511: Return values from a graphical function
205
Sub ID NA-MAAB: No recommendations
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a The return value for graphical functions shall be set in -
one place only.
【Correct】
【Incorrect】
Rationale
Sub ID Description
Modifications to the output name is limited to prevent the changes from being
a
missed or overlooked.
206
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Calls from a graphical function to itself and calls between -
graphical functions shall be prohibited.
【Correct】
Processing is performed within the graphical function.
【Incorrect】
The graphical function is calling itself.
Rationale
Sub ID Description
Readability decreases. Deviation from the rule can cause unintended overflows and
a
infinite loops.
207
na_0042: Usage of Simulink functions
Rule ID: Title na_0042: Usage of Simulink functions
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a When using [Simulink Function] in [Chart], one or more of -
the following conditions shall be met.
Input/output variables shall use only local [Chart] data
in the [Simulink Function].
Input/output variables shall use only local [Chart] data
and input data in the [Simulink Function].
[Simulink Function] shall be called from multiple
places in [Chart].
[Simulink Function] shall not be called at every time
step.
【Correct】
[Simulink Function] lookup1D is not called from every time step and, therefore, can
be used.
【Incorrect】
[Simulink Function] lookup1D is called from every time step and, therefore, cannot
be used. (out is the Stateflow output data)
Rationale
Sub ID Description
To improve model readability, the use of [Simulink Functions] should be used with
a
caution in charts.
208
na_0039: Limitation on Simulink functions in Chart blocks
Rule ID: Title na_0039: Limitation on Simulink functions in Chart blocks
Sub ID NA-MAAB: a
Recommendations JMAAB: a
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a Stateflow blocks shall not be used in [Simulink Functions] -
that are included in Stateflow [Chart].
【Incorrect】
Rationale
Sub ID Description
a Readability decreases and can result in design errors.
209
5. MATLAB
MATLAB Appearance
Example:
210
Rationale
Sub ID Description
Improves readability, model simulation, testability, and workflow
a
Code generation may not be possible.
211
function ErrorFlag =
EngineFaultEvaluation(EngineData,ErrorFlag_In)
%#codegen
RPM_HIGH = 10000;
RPM_LOW = 10;
HIGHRPMFAULT = 2^1;
LOWRPMFAULT = 2^2;
ErrorFlag = ErrorFlag_In;
if EngineData > RPM_HIGH
ErrorFlag = bitor(ErrorFlag,HIGHRPMFAULT);
end
if EngineData < RPM_LOW
ErrorFlag = bitor(ErrorFlag,LOWRPMFAULT);
end
end
function ErrorFlag = WheelFaultEvaluation(WheelData,ErrorFlag_In)
%#codegen
SLIP_HIGH = 1000;
WHEELSLIP = 2^3;
ErrorFlag = ErrorFlag_In;
if WheelData > SLIP_HIGH
ErrorFlag = bitor(ErrorFlag,WHEELSLIP);
end
end
【Incorrect】
This type of pattern cannot be used when the rule is applied.
function EngineFaultEvaluation(EngineData)
%#codegen
global ErrorFlag_DataStore
RPM_HIGH = 10000;
RPM_LOW = 10;
HIGHRPMFAULT = 2^1;
LOWRPMFAULT = 2^2;
if EngineData > RPM_HIGH
ErrorFlag_DataStore =
bitor(ErrorFlag_DataStore,HIGHRPMFAULT);
end
if EngineData < RPM_LOW
ErrorFlag_DataStore =
bitor(ErrorFlag_DataStore,LOWRPMFAULT);
end
end
212
function WheelFaultEvaluation(WheelData)
%#codegen
global ErrorFlag_DataStore
SLIP_HIGH = 1000;
WHEELSLIP = 2^3;
if WheelData > SLIP_HIGH
ErrorFlag_DataStore =
bitor(ErrorFlag_DataStore,WHEELSLIP);
end
end
Rationale
Sub ID Description
When a data store is used, the readability of the data flow decreases and can lead
a
to errors in the update reference timing.
【Incorrect】
Rationale
Sub ID Description
213
When an enumerated type does not have a clearly defined a default value, the first
a enumeration string that is described will be defined as the default, which may not be
as intended.
MATLAB Usage
na_0016: Source lines of MATALAB Functions
Rule ID: Title na_0016: Source lines of MATALAB Functions
Sub ID NA-MAAB: a
Recommendations JMAAB: Not supported
MATLAB® Version All
Rule
Sub ID Description Custom Parameter
a The length of MATLAB functions shall be limited. This Maximum effective lines
restriction applies to MATLAB Functions that reside in the of code per function
Simulink block diagram and external MATLAB files with
a .m extension.
214
a The number of sub-function levels shall be limited, Maximum function call
typically to three levels. levels
215
Rationale
Sub ID Description
MATLAB functions store strings as character arrays.
As a result, storing strings of different lengths in the same variable does not support
a
dynamic memory allocation, which prevents the strings from being stored.
(Consider using enumerated types when a string is used in [Switch Case])
【Correct】
216
【Incorrect】
Rationale
Sub Description
ID
Improves model simulation and testability.
a
Code generation may not be possible.
217
【Incorrect】
mpt Signal description (the same also applies to mpt.Parameter).
0011
Rationale
Sub ID Description
Since comment symbols /* and */ are automatically assigned in the generated code,
a
comments can be unintentionally nested and behave differently than expected.
218
6. Glossary
This section provides clarification of terms that are used in the guidelines.
Terms Definition
When modifications have not been made, this term refers to constants
Parameters
that are defined in the base workspace/model workspace.
Built-in MATLAB MATLAB functions and scripts.
functions
Reserved MATLAB
words
All blocks (Type=Block), including:
• Subsystems
• Models
Block • charts (unless otherwise stated).
Standard Simulink library blocks are divided into two categories:
• Basic blocks
• Structural subsystems.
Built-in blocks in the standard Simulink library.
Blocks with undefined internal processing, such as subsystems, are not
considered basic blocks.
Basic Blocks
220
7. Determining Guideline Operation Rules
This section provides general information about identifying which guidelines to adopt and the application
of these guidelines to your project.
MATLAB/Simulink Version
The version of MATLAB/Simulink used at each development stage is determined at the start of the
project. That version must be used by everyone during that development stage.
Different MATLAB versions can be used for different stages in the development process. For example,
you can generate and verify the code in R2017b and then use Simulink Design Verifier to develop test
cases R2020a.
It is necessary to regularly check the bug report published by MathWorks
(https://www.mathworks.com/support/bugreports). Depending on the bug, a version change may be
required; a decision that can be reversed if necessary. During this evaluation, it is important to consider
Deleted Simulink
Check Chapters.msg
risk from both:
• Malfunctions that result from a bug
• Result from upgrading the version.
It is necessary to always have a process that allows adaptation to the latest version and to appropriately
evaluate and judge what is the safest option.
MATLAB/Simulink Settings
MATLAB/Simulink settings shall adhere to the project. It is important that Simulink settings that affect
appearance are applied consistently across the project.
Usable Blocks
There are many blocks in Simulink, however, not all are suitable for all aspects of a project. For
example, only some blocks are suitable for generating production-quality code. Or, depending on the
block, a function using a combination of basic blocks can be represented by using one block. Usable
blocks and design should be defined and limited to the requirements and specifications of the project.
221
Note: Significantly limiting the number of available blocks can cause adverse effects, such decreased
readability due to variation within the descriptions for the same function, decreased code efficiency, and
increased user libraries.
Note: You must register custom blocks in the project’s user library.
Configuration Parameters
Hardware implementation settings
Describes model system hardware characteristics, including products and test hardware
configuration setup for simulation and code generation.
Configure these parameters so they are compatible with the microcomputer that the project uses.
Unintended utility functions can be inserted if signed integer division rounding is undefined.
222
• Unify description styles
• Anticipate in advance the man-hours needed to correct models
• Measuring tendencies, such as where to place blocks that have feedback status variables ([Unit
Delay]), whether [Unit Delay] should be inside or outside the subsystem, or whether [Abs] should
be set on the output side of the subsystem, and if it should process at the input side after
receiving a signal.
Setting the guideline rule application field and the clarifying the exclusion condition
The field to which the guidelines apply must be determined. For example, guidelines can be:
• Limited to a model that represents the AUTOSAR field of application
• Applied to a general software field, such as where models implement interrupts (add processes
that prohibit interruption during calculation).
• Specific to fields where general engineers edit the models. The intention of these rules is to
ensure that the models are easily understandable in those fields.
Note: Specialized fields can be excluded from the constraints of these guidelines by limiting the
scope and applying unique set of guidelines that are specific in this environment.
Specialized fields, such as those where modelers design custom library blocks, are not fields that are
typically targeted by these guidelines.
Furthermore, when having a control model that is operated with Rapid Control Prototyping (RCP), the
entire model should not be set as a target; instead, the field needs to be limited. It is necessary to
generate the code and review the areas that are implemented in the built-in microcomputer as well as
the areas that are not. These guidelines do not apply to control models such as those scheduler models
that are made solely for RCP and are not implemented, or for interface sections with blocks that
correspond to drivers such as CAN and PWM signals for operating actual machines.
223
When evaluating the change request, first listen to the needs of the modeler and determine the root
cause of the request. When the request is based on the user not understanding block usage or a
guideline rule, training should occur instead of revising the rule.
The procedure to relax the rules as needed should be implemented when there are restrictions due to
company objectives and control specifications or hardware (such as microcomputers).
224
8. Model Architecture Explanation
This section provides a high-level overview of model architecture that is suitable for model-based
development without specifying specific rules.
Either Simulink or Stateflow can be used to model specific parts of control, however, the application of
either product in the development workflow is based on the user’s understanding of the underlying
algorithms and, ultimately, comes down to the organization to determine which tool is best suited for their
needs. Determining whether Simulink or Stateflow should be used for design should be determined by a
group of people in accordance with the task. Whether implementation in Stateflow is done by using state
transitions or with flow charts should also be specified.
In most cases, Stateflow is less efficient with regards to RAM. Therefore, Simulink has an advantage in
computations that use simple formulas. In addition, Simulink is more advantageous for situations where
state variables are operated with simple flip-flops and [Relay]. When evaluating whether to use Simulink
or Stateflow in a project, these topics should be taken into consideration:
• Increasing RAM: There must always be a RAM available for visualization of Stateflow inputs, outputs
and internal variables.
• Equation error handling: When general computational formulas are used internally, the user designs
ways to prevent overflow.
• Splitting and separating functions: When performing calculations that use Simulink outside of
Stateflow, there is a possibility that they may split, thus reducing readability. There are also times
where readability may improve. This can be difficult to judge.
There are cases where Stateflow has more efficient code than Simulink for optimum expressions that
are close to code, but most of these result in a model that is difficult to understand. If code already exists,
it is more advantageous to use S-functions instead of Stateflow modelling. Stateflow can note
computations where specific arrangements are specified, or computations using for-loops, more efficiently
than Simulink, but in recent years it has also become convenient to use MATLAB language for
descriptions. If needed, consider using MATLAB language for modelling.
For Stateflow models, when dealing with states as described below, readability improves by describing
them as state transitions:
• Different output values are output for identical inputs.
• Multiple states exist (as a guide, three or more).
• States with meaningful names instead of just numbers.
• Inside a state, initialization (first time) and differentiation during execution (after the second time)
is required.
For instance, in flip-flop circuits, different values are outputted for inputs. State variables are limited to 0
and 1. However, a meaningful name cannot be added to each state simply by retaining Boolean type
numbers. There is also no distinction between initialization and execution within the state. Thus, only one
flip-flop applies out of the four above, so Simulink can be said to be more beneficial.
In Stateflow, situations that can be represented as states are implemented as state transitions and
conditional branches that are not states are implemented as flow charts. Truth tables are classified as a
conditional branch implementation method.
When designing states as state transitions by using Stateflow, “Classic” should be selected as the state
machine type so that it is implemented as software into the control system’s embedded micro controller.
HDL Coder is supported by Stateflow. If using HDL Coder, Mealy or Moore must be selected., Moore
mode is more appropriate when protection is required against internal electric leaks.
Note: HDL Coder use cases are not described in these guidelines.
225
Hierarchical Structure of a Controller Model
This section provides a high-level overview of the hierarchical structuring in a basic model, using a
controller model as an example.
Types of Hierarchies
When building hierarchies, division into subsystems for the purpose of saving space within the layer shall
be avoided.
Top Layer
Layout methods for the top layer include:
• Simple control model — Represents both the function layer and schedule layer in the same layer.
Here, function = execution unit. For example, a control model has only one sampling cycle and all
functions are arranged in execution order
• Complex control model Type α — The schedule layer is positioned at the top. This method makes
integration with the code easy, but functions are divided, and the readability of the model is
impaired.
• Complex control model Type β — Function layers are arranged at the top and schedule layers are
positioned below the individual function layers.
226
Example Schedule layer
Type α
Function layer Function layer
C1 C2
S1
S2
S1 C1
S2 C2
227
Example Schedule layer
Type α
Function layer Function layer
C1 C2
S1
S2
S1 C1
S2 C2
Schedule Layers
When scheduling layers:
• System sampling intervals and execution priority shall be set. Use caution when setting multiple
sampling intervals. In connected systems with varying sampling intervals, ensure that the system
is split for each sampling interval. This minimizes the RAM needed to store previous values in
the situation where the processing of signals values differs for fast cycles and slow cycles.
• Priority ranking shall be set. This is important when designing multiple, independent functions.
When possible, computation sequence for all subsystems should be based on subsystem
connections.
• Two different types of priority rankings shall be set, one for different sampling intervals and the
other for identical sampling rates.
There are two types of methods that can be used for setting sampling intervals and priority rankings:
• For subsystems and blocks, set the block parameter {sample time} and block properties
{priority}.
• When using conditional subsystems, set independent priority rankings to match the scheduler.
Patterns exist for many different conditions, such as the configuration parameters for custom
sampling intervals, atomic subsystem settings, and the use of model references. The use of a
specific pattern is closely linked to the code implementation method and varies significantly
depending on the status of the project.
Models that are typically affected include:
• Models that have multiple sampling intervals
• Models that have multiple independent functions
• Usage of model references
• Number of models (and whether there is more than one set of generated code)
• For the generated code, affected factors include:
o Applicability of a real-time OS
o Consistency of usable sampling intervals and computation cycles to be implemented
o Applicable area (application domain or basic software)
o Source code type: AUTOSAR compliant - not compliant - not supported.
228
o RAM, ROM margins (specifically RAM)
Block groups are arranged horizontally and are given a provisional meaning. Red borders, which
signify the delimiter for processing that is not visible, correspond to objects called virtual objects. Using
annotations to mark the delimiters makes it easier to understand.
Intermediate Output
processing processing
Input
processing
Control flow layers can co-exist with blocks that have a function. They are positioned between the sub-
function layer and the data flow layer.
229
Control flow layers are used when:
- The number of blocks becomes too large
- All is described in the data flow layer
- Units that can be given a minimum partial meaning are made into subsystems
Placement in the hierarchy organizes the internal layer configuration and makes it easier to understand. It
also improves maintainability by avoiding the creation of unnecessary layers.
When the model consists solely of blocks and does not include a mix of subsystems, if the horizontal
layout can be split into input/intermediate/output processing, it is considered a control flow layer.
Selection Layers
When modeling selection layers:
• Selection layers should be written vertically or side-by-side. There is no significance to which
orientation is chosen.
• Selection layers shall mix with control flow layers.
When a subsystem has switch functions that allow only one subsystem to run depending on the
conditional control flow inside the red border, it is referred to as a selection layer. It is also described as
a control flow layer because it structures input processing/intermediate processing (conditional control
flow)/output processing.
In the control flow layer, the horizontal direction indicates processing with different significance. Parallel
processing with the same significance is structured vertically. In selection layers, no significance is
attached to the horizontal or vertical direction, but they show layers where only one subsystem can run.
For example:
Switching coupled functions to run upwards or downwards, changing chronological order
Switching the setting where the computation type switches after the first time (immediately after
reset) and the second time
Switching between destination A and destination B
The horizontal
sequence
control flow layer
230
Data Flow Layers
A data flow layer is the layer below the control flow layer and selection layer.
A data flow layer represents one function as a whole; input processing, intermediate processing and
output processing are not divided. For instance, systems that perform one continuous computation that
cannot be split.
Data flow layers cannot co-exist with subsystems apart from those where exclusion conditions apply.
Exclusion conditions include:
• Subsystems where reusable functions are set
• Masked subsystems that are registered in the Simulink standard library
• Masked subsystems that are registered in a library by the user
When input processing and intermediate processing cannot be clearly divided as described above,
they are represented as a data flow layer.
A data flow layer becomes complicated when both the feed forward reply and feedback reply from the
same signal are computed at the same time. Even when the number of blocks in this type of cases is
large, the creation of a subsystem should not be included in the design when the functions cannot be
clearly divided. When meaning is attached through division, it should be designed as a control flow layer.
The configuration is affected significantly when the tasks of the embedded micro controller differs from
those modeled by Simulink.
231
8.3.1.1. Scheduler Settings in Embedded Software
The scheduler in embedded software has single-task and multi-task settings.
Function 1
Function 2 -1
-2
-3
-4
Function 3 -1
-2
-3
-4
-5
232
2. Enter sampling period, offset” values in the subsystem block {Sample Time}” field. A subsystem for
which a sampling period can be specified is an atomic subsystem.
2msec
8msec
10msec
Function 1
Function 2
Function 3
It is important that computations are completed within the cycle, including slow tasks. When the
processing of a high priority computation finishes and the CPU is available, the computation for the
system with the next priority ranking begins. A high priority computation process can interrupt a low
priority computation, which is then aborted so the high priority computation process can execute first.
233
8.3.1.2. Effect of Connecting Subsystems with Sampling Differences
If subsystem B with a 20 msec sampling interval uses the output of subsystem A with a 10 msec
sampling interval, the output result of subsystem A can change while subsystem B is computing. If the
values change partway through, the results of subsystem B’s computation may not be as expected. For
example, a comparison is made in subsystem B’s first computation with the subsystem A output, and the
result is computed with the conditional judgment based on this output. At this point, the comparison
result is true. It is then compared again at the end of subsystem B; if the output from A is different, then
the result of the comparison can be false. Generally, in this type of function development it may happen
that the logic created with true, true has become true, false, and an unexpected computation result is
generated. To avoid this type of malfunction, when there is a change in task, output results from
subsystem A are fixed immediately before they are used by subsystem B as they are used in a different
RAM from that used by the subsystem A output signals. In other words, even if subsystem A values
change during the process, the values that subsystem B are looking at is in a different RAM, so no effect
is apparent.
When a model is created in Simulink and a subsystem is connected that has a different sampling
interval in Simulink, Simulink automatically reserves the required RAM.
However, if input values are obtained with a different sampling interval through integration with hand-
coded code, the engineer who does the embedding work should design these settings. For example, in
the RTW concept using AUTOSAR, different RAMs are all defined at the receiving and exporting side.
2msec
8msec
10msec
If Function 2 uses computation results of
Function 1, computation results for Function
Function 1 1 do not change during computation for
Function 2 -1 Functions 2-1, 2-2, 2-3, but there is a
-2 possibility that Functions 2-1, 2-2, 2-3 use
-3 different values that have been computed on
-4 the respective different time axes.
Function 3 -1
-2 A different RAM should be allocated for signal
-3 values with a different rate.
-4
-5
234
2msec
If Function 2 uses
8msec computation results of
Function 1, it is possible that
10msec
computation results from
Do not immediately Function 1 will replace them
use values that are while Function 2 is
Function 1 being updated. computing.
Function 2 For that reason,
computation results that
Function 3 vary at the point when
computation starts for each
The value should be held at the sampling are generally
beginning of the task. stored in a different RAM.
235
9. Appendices
Simulink Functions
9.1.1.1. Blocks with State Variables
Blocks with state variables are primarily grouped into Simulink and discrete types.
For most of these blocks, the user can set the state attributes and initial values by using the block
parameters. A conditional subsystem can have state variables, depending on the structure pattern.
In the conditional subsystem, subsystem A is calculated when the condition is satisfied. When is not
satisfied, subsystem B is calculated instead of subsystem A, regardless of the existence of any state
variables in subsystem A.
The reset action in a recalculation can be specified by using the {Action Port} setting.
The behavior of subsystem A when using [Switch] and a conditional control flow is listed in the
following tables. Familiarize yourself with these behaviors to determine which structure, [Switch], or
conditional subsystem is most suitable for the intended purpose.
Behavior of subsystem A
Control (in subsystem A) Switch Conditional
port State variables subsystem
condition
Hold No Executed Executed
Yes
Not hold No Not executed Not executed
Yes Minimally-processed
*Executed calculations related to the
state variables
9.1.1.3. Subsystem
A subsystem is used for compiling various blocks and subsystems.
Subsystems can also be used for other purposes. Usage methods that are not functional subsystems
include:
• Mask display of the subsystem is used to describe the outline or display fixed form documents,
such as "classified"
• The open functions (callback functions in the block properties) of the subsystem is used for
running several tools or displaying explanatory text separate from the model
• Subsystems whose setting have changed to a mask subsystem (a subsystem that was simply
set to NoReadOrWrite) by a user with administrative rights to make a change, but other users
cannot see the content.
These non-typical subsystems are outside of the scope of the guidelines and, if excluded, should be
put on an exclusion list managed within the project.
See guidelines: jc_0201, jc_0243, db_0143, db_0144, db_0141, jc_0653, jc_0171, jc_0602, jc_0081,
db_0081
Code can be generated by associating a signal name with a signal object (Simulink object or mpt
object). Type setting is configured through the data dictionary, setting of the storage class is optional.
The recommended data type settings for these blocks include:
For [Inport], set the {Data type} to ”auto”
For [Outport], set the {Data type} to ”auto”
For [Sum], set the output {Data type} to ”Inherit via back propagation”
Signals that do not fulfill the conditions as a vector can only be grouped as a bus signal. [Bus
Selector] shall be used only with bus signal inputs. It shall not be used to extract a scholar signal from
a vector signal.
Example
The following is an example of a vector signal:
Types of vector Size
Row vector [1 n]
Column vector [n 1]
238
Types of vector Size
Location
Acceleration
Pressure
Actuator bus
Stateflow Functions
9.2.1.1. Operators Available for Stateflow
For additional information about the Stateflow operators, see “ Supported Operations for Chart Data”
in the Stateflow user help.
Related ID: na_0001, jc_0655
A flow chart cannot maintain its active state between updates. As a result, a flow chart always ends
at a “terminating junction” (a connective junction that has no valid outgoing transitions).
239
In contrast, a state transition diagram stores its current state in memory to preserve local data and
active state between updates. As a result, state transition diagrams can begin executing where they
left off in the previous time step. This means that state transitions are suitable for modeling reactive or
supervisory systems that depend on history.
Flow Chart
Mixture of flow charts and state transition diagrams with self-transition has more strict constraints.
240
Example of flow chart with self-transition
State Transition Diagram
State transition
diagram
9.2.1.3. Backtrack
This example shows the behavior of transitions with junctions that force backtracking behavior in flow
charts. The chart uses implicit ordering of outgoing transitions.
Initially, state A is active and transition conditions c1, c2, and c3 are true and c4 is false:
1. The chart root checks to see if there is a valid transition from state A.
There is a valid transition segment marked with the transition condition c1 from state A to a
connective junction.
2. Transition condition c1 is true, so action a1 executes.
3. Transition condition c3 is true, so action a3 executes.
4. Transition condition c4 is not true and, therefore, the control flow backtracks to state A.
5. The chart root checks to see if there is another valid transition from state A.
There is a valid transition segment marked with the transition condition c2 from state A to a
connective junction.
6. Transition condition c2 is true, so action a2 executes.
7. Transition condition c3 is true, so action a3 executes.
8. Transition condition c4 is not true and, therefore, the control flow backtracks to state A.
9. The chart goes to sleep.
To resolve this issue, consider adding unconditional transition lines to terminating junctions.
The terminating junctions allow flow to end if either c3 or c4 is not true. This design leaves state A
active without executing unnecessary actions.
241
See guidelines: jc_0751 and jc_0773
Done correctly, as with the line below, embed a transition condition that is intentionally not positioned
at the termination of the external flow chart; it should be described so that the transition line from a to b
is evaluated after the flow chart has been executed.
This enables the external flow chart to execute before the transition, and to be evaluated using the
most recent value at the instant of the transition. Note that this chart contains a dead path where the
transition condition will never hold, which can cause an error when the specification is changed in the
future. Use this chart structure with caution.
242
In contrast, the following flow chart is inside a state, which means that the internal flow chart is
always calculated when executing state a and can be described as an easily comprehensible structure
without dead paths.
However, it should be noted that, as a performance characteristic, when state a is executed, the
transition from a to b is evaluated in the cycle following that in which the internal flow chart is
calculated.
Due to this characteristic, the timing of the execution of calculations and transitions for the external
flow chart may be off. Use with caution.
243
9.2.1.5. Pointer Variables
Describe using the example model sf_custom.
---------my_header.h--------------
#include "tmwtypes.h"
---------------my_function.c--------------
#include "my_header.h"
#include <stdio.h>
real_T my_function(real_T x)
{
real_T y;
y=2*x;
return(y);
}
244
Initialization
9.3.1.1. Initial Value Setting in Initialization
When a signal needs to be initialized, the initial values shall be set correctly.
When initial values are set inside a block, use an initial value list that includes annotations so you can
visually confirm the initial values input.
Discrete block groups, such as [Unit Delay] and [Data Store Memory] have state variables.
In the case of automatic code generation, the signal name, type, and initial value can be set for
state variables by matching it to the signal in the data dictionary (associated with Simulink signal
objects). When using a signal defined in the data dictionary for a state variable, the respective initial
values should conform to the same value.
When using a signal defined in the data dictionary for a state variable
For discrete blocks, such as a [Unit Delay] and [Data Store Memory], settings are performed not
when using signals defined in the data dictionary for the block output line, but for the state variables
inside the block. Even when the signal name of the data dictionary is assigned to the signal line,
RAM is reserved in duplicate, which is a waste of RAM.
245
【Correct】 When the signal is defined for the 【Incorrect】 When the signal is defined for
state variables inside the block. the output signal of the block that has state
variables.
Signal objects that are defined in the Workspace can be automatically associated with signal objects
and signal names of the same name by using disableimplicitsignalresolution (model
name. However, for state variables inside the block, they are associated with the state variables inside
the block and the signal name of the same name. If a globally set signal is associated with two
variables at the same time, it is better to perform settings so that the state variables inside a block and
the signal label on the signal line have different names, otherwise the model cannot be simulated.
When setting the initial value during initialization, the init function is called to set the signal to
either the value inside of the block or to the initial value that is defined in the data dictionary.
246
Next, the step function (the data flow executive function) is executed. Here, the external input
value is set as the initial value.
When modelling, be attentive to the execution functions and execution timing for initialization.
init function
Set the specified initial
value to the signal
Exception:
When there are successive blocks with initial values and the settings for each block are not needed
to clearly show the signal’s initial value.
247
【Correct】 Initial value set in mpt object
【Incorrect】 Despite the requirement for an initial value setting, it is not shown anywhere.
248
Miscellaneous
9.4.1.1. Atomic Subsystems and Virtual Subsystems
There are two types of subsystems, Virtual subsystem and Atomic subsystems. The primary
difference between these subsystems is whether the subsystem is treated as a single execution unit.
The virtual subsystem is the default subsystem block.
In a model, the border for a Virtual subsystem is thin as compared the border for the Atomic
subsystem, which is thick and bold.
Virtual Subsystem
A block that provides a visual representation is known as a "virtual block. ". For example, [Mux] that
compiles several signal lines, [From] that hands out the signal, and [Goto] blocks all correspond to a
virtual block. Since the subsystem block in the default setting only constitutes a visual hierarchical
structure, these blocks are considered virtual blocks. The subsystem is referred to as a virtual
subsystem.
Consider a subsystem that consults an external calculation result within a subsystem, as shown in
the following example. This system is calculated from these four equations.
temp1= in1 + in2
temp2= in3 + in4
out1= in1 + in2 + temp2
249
out2= temp1 + in3 + in4
With virtual subsystem, it is
possible to consult the values
within other subsystems.
Virtual subsystem
Atomic subsystems prohibit the direct referencing of the interim calculation results to other
subsystems.
Atomic subsystem
250
• Depending on the relationship before and after, a static RAM section should be secured
inside the subsystem for the output signal.
• Atomic subsystems (including the addition of function settings) should be used with caution.
Factor setting will not simply have a factor name inserted within a C code. It should be
acknowledged that it is described as a mathematically independent system and the
conditions under which an atomic subsystem can be used should be reviewed.
• Include the relationship with the structure layer; it is necessary to determine an operation rule
per project and to determine its relationship with the guideline rules.
if (If_Condition)
{
output_signal = If_Value;
}
else if (Else_If_Condition)
{
output_signal = Else_If_Value;
}
else
{
output_signal = Else_Value;
}
If, elseif, else construct using Action Subsystem
switch (PRNDL_Enum)
{
251
case 1
TqEstimate = ParkV;
break;
case 2
TqEstimate = RevV;
break;
default
TqEstimate = NeutralV;
break;
}
switch (Selection)
{
case 1:
output_signal =
look1_binlxpw(In2,y1,x1,3U);
break;
case 2:
output_signal =
look1_binlxpw(In3,y2,x2,3U);
break;
case 3:
output_signal =
look1_binlxpw(In4,y3,x3,3U);
break;
default:
output_signal =
look1_binlxpw(In5,y4,x4,3U);
break;
}
252
Disjunctive normal form
output_signal = 1;
for (i=0; i>input_vector_size; i++) {
output_signal = output_signal *
input_vector[i];
}
Vector signal element division
output_signal = 1;
for (i=0; i>input_vector_size; i++) {
output_signal = output_signal /
input_vector[i];
}
Vector signal and parameter (scalar) addition
output_signal = 0;
for (i=0; i>input_vector_size; i++) {
output_signal = output_signal –
input_vector[i];
}
254
Retention of minimum value/maximum value
Not recommended: Using [If], [If Action Subsystem] for a simple if, elseif, else structure.
Recommended: For a complex if, elseif, else structure, use [If], [If Action Subsystem].
255
Not recommended: Using [Switch] for a complex if, elseif, else structure.
Appendix 6: Use of if, elseif, else Action Subsystem to Replace Multiple Switches
Frequent use of [Switch] for condition bifurcation shall be avoided. Instead, the {upper limit target} shall be
used (such as up to three levels). When the target value is exceeded, a conditional control flow using the
if, elseif, else Action Subsystem shall be used.
256
Recommended: By setting the fourth level as an if Action subsystem, nesting is limited to a single level.
257
Not recommended: Not dividing by using an if Action subsystem.
Use atomic subsystem + function setting when the C code limit is applied. In this case, there is no need to
use the if, elseif, else Action Subsystem, but the configuration of [Switch] can be split and encapsulated in
the subsystem.
Not recommended:
258
Recommended: Use a description method that avoids layering of [Switch] nesting
While provided as an example, If Action Subsystem a are not typically used for switching the fixed value.
In these Recommended and Not Recommended examples, the generated C code will be the same if the
user does not add a function conversion setting. (Confirmed in R2010b to R2013a) The C code is
unconstrained.
259
Appendix 7: Usage Rules for Action Subsystems Using Conditional Control Flow
An If Action subsystem shall not be used when the associated actions do not have a status variable.
Recommended
Recommended
Layering by using a subsystem does not occur because there is no internal state.
260
Recommended
An atomic subsystem is used to split either side of [Switch] without using an Action subsystem.
261
Not recommended:
Layering through the use of an unnecessary Action subsystem.
If a function can be achieved by using the Action subsystem, then layering using the Action subsystem is
not performed.
In the Not Recommended example, when the lowest level [Unit Delay] on the third level is initialized, the
conditional subsystem initialization is first executed one time on the upper first level, and then again on the
second level for a total of two times of initial value settings. To prevent the generation of unnecessary
262
code, it is recommended that listing not be made in conditional subsystems that reside in levels where the
state variable does not exist.
This is based on the concept that the model complexity is reduced by dropping to a level. The purpose of
the rule is to avoid the execution of unnecessary initializations.
For bifurcation of systems where the bifurcation condition nest has a deep structure, split by using function
conversions to decrease the code bifurcation nesting. Functions before and after [Switch] are divided into
respective subsystems, and function settings are applied to the atomic subsystem+function. Be aware, it
is possible that this may result in unintentional implementation and unnecessary RAM requirements.
Recommended
Error information is incorporated into the model structure, allowing the user to review and respond to the
errors.
Not recommended:
Error information is not incorporated into the model structure.
263
Appendix 9: Flow Chart Patterns for Conditions
These patterns shall be used for conditions within Stateflow flow charts.
ONE CONDITION:
[condition]
[condition1 ...
&& condition2 ...
&& condition3]
[condition1 ...
|| condition2 ...
|| condition3]
264
CONDITIONS WITH SUBCONDITIONS:
(The use of different logical operators to connect
sub conditions is not allowed. The use of brackets
is mandatory.)
These patterns shall be used for condition actions within Stateflow flow charts.
265
action3; ...
These patterns shall be used for If constructs within Stateflow flow charts.
If construct
if (condition){
action;
}
if (condition) {
action1;
}
else {
action2;
}
266
If, elseif, else construct
if (condition1) {
action1;
}
else if (condition2) {
action2;
}
else if (condition3) {
action3;
}
else {
action4;
}
Cascade of if construct
if (condition1) {
action1;
if (condition2) {
action2;
if (condition3) {
action3;
}
}
}
267
Appendix 12: Flow Chart Patterns for Case Constructs
These patterns shall be used for case constructs in Stateflow flow charts.
Function Simulink pattern
selection = u1;
switch (selection) {
case 1:
y1 = 1;
break;
case 2:
y1 = 2;
break;
case 3:
y1 = 4;
break;
default:
y1 = 8;
}
c1 = u1;
c2 = u2;
c3 = u3;
if (c1 && ! c2 && ! c3) {
y1 = 1;
}
elseif (! c1 && c2 && ! c3) {
y1 = 2;
}
elseif (! c1 && ! c2 && c3) {
y1 = 4;
}
else {
y1 = 8;
}
for ( index = 0;
index < number_of_loops;
index++ )
{
action;
268
}
while ( condition )
{
action;
}
do
{
action;
}
while ( condition )
269
Appendix 14: State Machine Patterns for Conditions
These patterns shall be used for conditions within Stateflow state machines.
ONE CONDITION:
(condition)
(condition1 ...
&& condition2 ...
&& condition3)
(condition1 ...
|| condition2 ...
|| condition3)
These patterns shall be used for transition actions within Stateflow state machines.
action;
action1;
action2;
action3;
270
Appendix 16: Limiting State Layering
Within a single viewer (subviewer), multiple layering shall be limited by defining constraints for a single
view (subview). Subcharts shall be used to switch the screen when defined constraint goals are
exceeded.
Recommended
The fourth level is encapsulated in a subchart.
Not recommended:
The constraint goal is set to three levels, but Level_4_a and Level_4_b have more than three levels and
are nested.
271
Appendix 18: Function Call from Stateflow
If a state exists in the Function Call Subsystem of the call target, and a “reset” of the state is required
when the state of the caller becomes inactive, the caller shall use a bind action.