stateflow 변수 초기화 방법
25 views (last 30 days)
Show older comments
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
Walter Roberson
on 31 Oct 2024
According to https://www.mathworks.com/matlabcentral/answers/838503-persistent-data-in-stateflow#answer_708430
Variables you define as 'local' or as 'output' in the chart are persistent over chart executions.
Accepted Answer
Shivam Gothi
on 7 Nov 2024 at 6:40
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:
- Initialise variable “a” with 0.
- If “a<9”, then increment “a” and set “y=1”
- Keep on iterating step-2 untill the condition “a<9” holds true.
- 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!
More Answers (0)
See Also
Categories
Find more on Complex Logic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!