When you export chart-level functions, you extend the scope of your functions to other blocks in the Simulink® model. For an example, see Share Functions Across Simulink and Stateflow. You can export these functions:
Graphical functions
MATLAB® functions
Truth tables
Open the chart where your function resides.
In the Property Inspector, open the Advanced section.
Select Export Chart Level Functions.
If your function resides in a library chart, link that chart to your main model.
When you select Export Chart Level Functions, you
can call exported functions by using Simulink Caller blocks with dot
notation,
chartName.functionName
.
To call the exported functions throughout the model from any Stateflow® or Simulink Caller block, select Treat
Exported Functions as Globally Visible. Do not use dot notation to
call these functions. You cannot export functions with the same name.
Simulink functions can also be defined directly in the Simulink canvas. For more information, see Simulink Function.
Link library charts to your main model to export chart-level functions from
libraries
Do not export chart-level functions that contain unsupported inputs or
outputs
This example describes how to export functions in library charts to your main model.
Create these objects:
Add a model named main_model
, with a chart
named modChart
.
Add a library model named lib1
, with a chart
named lib1Chart
.
Add a library model named lib2
, with a chart
named lib2Chart
.
Create these graphical functions in the library charts:
For lib1Chart
, add this graphical
function.
For lib2Chart
, add this graphical
function.
In the Model Explorer, for each of the function inputs and
outputs, a
, b
, and
c
, set these properties:.
Size
to 1
Complexity
to
Off
Type
to
double
For modChart
, add a graphical function and a default
transition with a lib1_func
action.
For each chart, follow these steps:
In the Model Explorer, for each of the function inputs and
outputs, a
, b
, and
c
, set:
Size
to 1
Complexity
to
Off
Type
to
double
Open the Chart properties dialog box.
In the Chart properties dialog box, select Export Chart Level Functions and Treat Exported Functions as Globally Visible.
Click OK.
Drag lib1Chart
and lib2Chart
into
main_model
from lib1
and
lib2
, respectively. Your main model should look
something like this:
Each chart now defines a graphical function that any chart in
main_model
can call.
Open the Model Explorer.
In the Model Hierarchy pane of the Model Explorer,
navigate to main_model
.
Add the data x
and y
to the
Stateflow machine:
Select Add > Data.
In the Name column, enter
x
.
In the Initial Value column, enter
0
.
Use the default settings for other properties of
x
.
Select Add > Data.
In the Name column, enter
y
.
In the Initial Value column, enter
1
.
Use the default settings for other properties of
y
.
This step ensures that input and output data are defined globally to support exported graphical functions.
Open the Model Configuration Parameters dialog box.
In the Model Configuration Parameters dialog box, go to the Solver pane.
In the Solver selection section, make these changes:
For Type, select
Fixed-step
.
For Solver, select Discrete (no
continuous states)
.
In the Solver details section, make these changes:
For Fixed-step size, enter
1
.
Click OK.
This step ensures that when you simulate your model, a discrete solver is used. For more information, see Compare Solvers (Simulink).
When you simulate the model, these actions take place during each time step.
Phase | The object... | Calls the graphical function... | Which... |
---|---|---|---|
1 | modChart | lib1_func | Reads two input arguments x and
y |
2 | lib1_func | lib2_func | Passes the two input arguments |
3 | lib2_func | mod_func | Adds x and y and
assigns the sum to x |
To view the simulation results, add a scope to your model. Follow these steps:
Open the Simulink Library Browser.
From the Simulink/Sinks Library, select the Scope block and add it to
main_model
.
Open the Model Explorer.
In the Model Hierarchy pane, navigate to
modChart
.
Add the output data z
to the chart:
Select Add > Data.
In the Name column, enter
z
.
In the Scope column, select
Output
.
Use the default settings for other properties.
For modChart
, update the default transition action
to read as follows:
{x = lib1_func(x,y); z = x;}
In the model, connect the outport from modChart
to
the inport of the Scope block.
Double-click the Scope block to open the display.
Start simulation.
After the simulation ends, right-click in the scope display and select Autoscale.
The results look something like this: