state flow chart properties action language

50 ビュー (過去 30 日間)
VINAY
VINAY 2025 年 2 月 11 日 7:01
コメント済み: VINAY 2025 年 2 月 13 日 11:35
Action Language selected as Matlab, Cnt(counter) is updating and working fine.
when, Action Language selected as C, and if else ststement is replaced with flowchart (pattern) Cnt is not updating
if Any one faced this kind of issue before please help me

採用された回答

Nithin
Nithin 2025 年 2 月 13 日 10:18
Hi @VINAY,
You are encountering unexpected behavior because Stateflow does not recognize the pattern you added under the 'during' action; instead, it categorizes it under the 'entry' action. Consequently, the 'if-else' pattern executes before the statements in the 'during' action.
To better understand the execution flow, I included 'print' statements. Refer to the state below:
The output after executing is attached below:
From the output, it's evident that 'In Pattern' is printed before 'In During', indicating that the execution goes to the pattern blocks instead of 'during' action.
I understand you replaced 'if-else' statements with a pattern because the C-Action Language doesn't support conditional control statements. This behavior is expected even when using MATLAB as the Action Language with such patterns. You can leverage the Graphical Functions and call the function within the 'during' action. Refer to the below attached example:
The output below gives expected result:
For more information on implementing Graphical Functions, you may refer to the following documentation:
I hope this resolves your query.
  1 件のコメント
VINAY
VINAY 2025 年 2 月 13 日 11:24
Thank you Nithin, yes its resolved

サインインしてコメントする。

その他の回答 (1 件)

Aravind
Aravind 2025 年 2 月 13 日 11:16
Hi @VINAY,
When you incorporate a flowchart within a state in Stateflow, the if-condition flowchart is executed only when that state is first entered. This means it runs the code in the “en” section of the state, followed by the if-condition flowchart. In subsequent time steps, when the chart remains in the same state, the during section (marked as “du”) is executed instead, bypassing the if-condition flowchart. Consequently, the if condition is not re-evaluated, preventing the counter “Cnt” from updating. This issue occurs regardless of the action language, affecting both C and MATLAB.
Since if-conditions as code are not supported in Stateflow when using C as the action language, a workaround is to restructure the Stateflow chart using substates. Substates allow you to execute a flowchart within a specific state, enabling multi-level state complexity. More information on substates can be found here: https://www.mathworks.com/help/releases/R2022a/stateflow/ug/state-hierarchy.html
With substates, your counter implementation might look like this:
In this setup, “State1” is similar to what you have shared, containing the entry code for initializing variables “Cnt” and “Y.” After executing the “en” section, Stateflow proceeds to “SubState1,” where the if-condition is implemented as a flowchart. There are two transitions: a self-transition that remains within the same state until “Y” becomes true, and an exit transition that moves from “SubState1” to “State1” once “Y” is true and the counter has completed counting.
This approach can be used with C as the action language to address the issue of the “Cnt” variable not updating.
For more insights into using substates and their transition semantics, you can explore this example: https://www.mathworks.com/help/releases/R2022a/stateflow/ug/modeling-a-dc-motor-in-stateflow.html
I hope this helps resolve your question.
  1 件のコメント
VINAY
VINAY 2025 年 2 月 13 日 11:35
Thank you Aravind

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeComplex Logic についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by