MATLAB: How to access a global C struct variable defined in Custom Code inside Stateflow

stateflow

I would like to define a custom structure type in C code and also create a global variable of this type in my custom code. Is there a way to access this global variable inside of Stateflow?
So for instance, have a transition like this:
[myGlobal.var1 == 1]{out = myGlobal.var2[0];}
Where myGlobal is a structure defined in external code.

Best Answer

Yes we can do this. First we need to make sure we are using the C action language in Stateflow. Then we need to declare the global variable as extern in a header or something similar and include it in the model generated C code. This is simlar to what is done with "my_global" in the example below.
Finally we must specify the location of our custom C files in the Model Configuration>Simulation Target Pane
I have created a simplified example attached that shows how to Include a global variable of a custom struct type in Stateflow. Here we can access a struct variable "myGlobal" of custom struct type "myStruct" inside Stateflow.