Clear Filters
Clear Filters

Using data internal to simulink state block as condition in transition of stateflow chart

13 views (last 30 days)
I have made a finite state machine as a stateflow chart within a simulink model. The states are all "Simulink States." This is nice because the inputs and outputs of the chart as a whole can each be the inputs and outputs of the states. So as it moves between the simulink states, it is just transitioning between operating modes (see
I am currently using the method from that link where I access the simulink subsystem state variables. The problem is that I do not have any blocks in the simulink functions with states I care about, so I am just making a delay block and reading from its internal state. The delay block then feeds into a terminator. This feels like a hack. I wish there was a dedicated "internal state data block" that I could write to. This technically works, I am just looking to see if there is a better way.
I've considered using a "data store memory block," but that will be messy since all of my states are writing to a variable that I want to stay local to each of the states (plus I can't have a data store memory in a chart, and I would need to put the chart in a subsystem).
I've considered using outputs, but then that gives my chart outputs which really clutters things.
I've tried making these conditions as simulink function calls in the chart level, but they end up having so many inputs that it isn't worth it.
I've tried using events, but I am not sure this is the right call. I basically made a chart in the simulink state block that emitted an event when its called. This doesn't feel right because I still want my transitions from the simulink state block to be evaluated in order as they usually are (maybe it would still work that way and I am misunderstanding how events work)
I've tried mapping an output port from the state to a local data in the chart level (this would be an awesome solution, and one of the more intuitive ones), but it only lets me map outputs of simulink state blocks into outputs of the chart.
Anyway, I hope this was clear. Thank you for any help you can give.

Answers (1)

Yatharth on 29 Sep 2023
Hi Samuel,
I understand that you have implemented a finite state machine using Stateflow within a Simulink model. Simulink States provide a convenient way to represent operating modes and transition between them.
Regarding your concern about accessing Simulink subsystem state variables, it is understandable that using a “Delay” block as a workaround might feel like a hack. And I also understand your concern about using a “Data Store Memory” block and the potential messiness it may introduce, especially if you want to keep the variable local to each state. While there isn't a dedicated "Internal State Data” block in Simulink, there are alternative approaches you can consider:
  1. Simulink Bus objects: You can create a “Simulink Bus” object that contains all the variables you need for your internal state. Each state can have its own instance of the bus object, allowing you to keep the variables local to each state. You can then use “Simulink Bus Selector” blocks to access the specific variables you need within each state.
  2. Simulink Function blocks: “Simulink Function” blocks allow you to define reusable functions that can be called from multiple locations within your Simulink model. You can create a “Simulink Function” block for each state and define local variables within each function block. This way, each state will have its own set of local variables.
  3. S-Function block: If you require more flexibility and control over the internal state variables, you can consider using a MATLAB “S-Function” block. S-Function blocks allow you to write custom C or MATLAB code to implement your state machine logic. You can define and update your internal state variables directly within the “S-Function” block’s code.
I hope this helps.
  1 Comment
Samuel Kemp
Samuel Kemp on 2 Oct 2023
Sorry, I think there is a misunderstanding.
I want to access this "internal state data" from the chart level. This image shows how I am doing it now, where the wpPassed and wpReached values are internal states for unit delay blocks in the Line subsystem. This works great as far as I can tell, but I am surprised at how there is not a straightforward way to use the conditions like this on transitions from a state.

Sign in to comment.


Find more on Simulink Functions 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!