stateflow 변수 초기화 방법

39 views (last 30 days)
영진
영진 on 31 Oct 2024 at 0:14
Commented: 영진 on 7 Nov 2024 at 8:19
stateflow에서 위와 같이 모델링 할경우 아래와 같이 a 초기값을 선언해달라고 하는데.
c언어에서 처럼 static a=0 과 같이 local 변수지만 초기화만 할수 있는 방법이 없을까요?
state에서는 en : a=0으로 할수는 있을거 같은데
많이 사용하는 방법에 대해 문의 드립니다.
The data a was read before being written to. This error will stop the simulation. Transition in Chart 'untitled/Chart': [a < 9]
  3 Comments
영진
영진 on 31 Oct 2024 at 3:59
In stateflow, when modeling as above, it asks to declare the initial value of a as below.
Is there no way to initialize it as a local variable like static a=0 in C language?
I think it is possible to do en: a=0 in state,
but I am asking about a method that is commonly used.
Walter Roberson
Walter Roberson on 31 Oct 2024 at 5:24
Variables you define as 'local' or as 'output' in the chart are persistent over chart executions.

Sign in to comment.

Accepted Answer

Shivam Gothi
Shivam Gothi on 7 Nov 2024 at 6:40
Hello @영진,
As per my understanding, you are trying to implement the logic that is similar to “static a=0” in C language.
I was not able to reproduce the issue faced by you, but it seems that you are trying to implement the below logic:
  1. Initialise variable “a” with 0.
  2. If “a<9”, then increment “a” and set “y=1”
  3. Keep on iterating step-2 untill the condition “a<9” holds true.
  4. When “a” becomes greater than or equal to 9, then make the variables “a=0” and “y=0”
To implement above logic using Simulink “Chart, I made a small change in your flowchart model as shown below:
I have added a “State” block and attached the “default transition” with it. Inside the “state” block, I initialised the variable “a” with value of “0”
In the “symbols pane” (found under “modelling” tab), I selected the “Type” of variable “a” as “local” and the “Type” of variable “y” as “Output”. This created an output port as shown below.
Now, you can observe the value of variable “y” on scope as shown below.
Conclusion:
As long as the condition 'a < 9' holds true, the variable 'a' continues to increment, while 'y' remains set at the value of 1. Once the condition 'a < 9' is no longer met, both variables are reset, with 'a' returning to 0 and 'y' also resetting to 0.
(Note: Here, I configured the “sampling time” of the block “state” to “0.01” from its “block parameters”)
I have also attached the Simulink model with this answer.
I hope it helps!
  1 Comment
영진
영진 on 7 Nov 2024 at 8:19
Thank you so much.

Sign in to comment.

More Answers (0)

Categories

Find more on Complex Logic in Help Center and File Exchange

Tags

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!