stateflow 변수 초기화 방법

38 visualizzazioni (ultimi 30 giorni)
영진
영진 il 31 Ott 2024 alle 0:14
Commentato: 영진 il 7 Nov 2024 alle 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 Commenti
영진
영진 il 31 Ott 2024 alle 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 il 31 Ott 2024 alle 5:24
Variables you define as 'local' or as 'output' in the chart are persistent over chart executions.

Accedi per commentare.

Risposta accettata

Shivam Gothi
Shivam Gothi il 7 Nov 2024 alle 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 Commento
영진
영진 il 7 Nov 2024 alle 8:19
Thank you so much.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Complex Logic in Help Center e File Exchange

Tag

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by