Is there a simple example that uses structures in Stateflow?

2 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
You can find information about using structures in Stateflow in the 'Working with Structures and Bus Signals in Stateflow' section of the Stateflow documentattion.
The five attached files comprise a simple example of the use of structures with custom C code in Stateflow.
The Simulink model (structdemo.mdl) includes a Stateflow chart which has a call to a custom C function. This function, "myfunction" is defined by a C-file (myfunction.c) and declared in a corresponding header file (myfunction.h). It accepts a pointer to a structure as one of its input arguments. Structures in Stateflow can only be passed via pointers.
The type of the structure in the example (MYSTRUCTTYPE) is defined in two places:
- as a Simulink.Bus object in the base workspace, and
- as a typedef in a C header file (mystructtype.h).
Simulink requires the structure be defined in both of these two places. Furthermore, these definitions must match:
- The Simulink.Bus object must have the same name as the typedef
- The two must have the same field names
- The fields' data types must be the same
Additional details about setting up the model are included below:
1. Creating the Simulink.Bus object:
In this example, the Simulink.Bus object was originally created and configured using the Simulink Bus Editor, which can be launched by executing:
buseditor
at the MATLAB prompt. In addition configuring the bus to match the typedef definition, mystructtype.h, was added to the bus' "Header file that contains typedef for bus" field in the Bus Editor.
After this bus object was created, it was saved to a MAT-file (workspacevars.mat) by executing
save workspacevars.mat
at the MATLAB prompt. Then, the statement
load workspacevars.mat
was added to the model's "InitFcn" callback. This ensures the appropriate Simulink.Bus object is created every time the model is run. This is a convenient way to introduce the Simulink.Bus variable into the base workspace, but it is not necessary to use this particular method.
2. Declaring the external C code to Simulink:
To incorporate the externally-defined custom code into the Simulink model, the two lines:
#include mystructtype.h
#include myfunction.h
have been added to the "Include Code" section of the Simulation Target options. This can found via the "Tools -> Open Simulation Target -> Target Options -> Include Code" menu item in the Stateflow chart.
Similarly,
myfunction.c
was added to the "Source Files" tab.

More Answers (0)

MathWorks Support

Categories

Find more on Composite Interfaces in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!