Simulink^{®} functions have an interface with input and output arguments similar to
programming languages. You create the function definitions:

Graphically, using Simulink blocks in a Simulink Function block, or Stateflow

^{®}state transitions in a graphical function exported from a Stateflow chart.Textually, using MATLAB

^{®}code in a MATLAB function exported from a Stateflow chart.

Set up a Simulink Function block to receive data through an input argument from a function caller. Multiply the input argument by 2, and then pass the calculated value back through an output argument.

Add a Simulink Function block from the User-Defined Functions library into your model.

Open the Library Browser by selecting the Library Browser toolbar button .

On the block, enter this function prototype to set the function name to

`timestwo`

, the input argument to`u`

, and the output argument to`y`

:y = timestwo(u)

Double-click the block to open the subsystem defining the function.

Add a Gain block and set the

**Gain**parameter to`2`

.

Set up a graphical function in a Stateflow chart to receive data through an input argument from a function caller. Multiply the input argument by 2, and then pass the calculated value back through an output argument. Set chart parameters to export the function to a Simulink model.

**Define a Graphical Function. **Create a graphical function in a Stateflow chart. Define the function interface and function definition.

Add a Stateflow Chart to your Simulink model. From the Simulink Editor menu, select

**File**>**New**>**Chart**.Drag the new chart to your model. Double-click to open the chart.

Add a graphical function. From the left-side toolbar, click and drag the graphical function icon onto the chart.

Define the function interface. In the function box, replace the

`?`

with the function interface`y = timestwo_gf(x)`

.Define the function. Click the transition arrow and replace the

`?`

with the MATLAB code`{y = 2*x}`

.Add a transition in the chart. Connect the chart input to a Sine Wave input and the output to a Scope.

Run a simulation to test the function.

**Set Argument Parameters for a Graphical Function. **Specify the size, complexity, and type of the function input and output arguments. A
chart can export only functions with fully specified prototypes.

Open Model Explorer. In the left pane, select the graphical function.

From the

**Column View**list in the middle pane, select`Stateflow`

. Select the filter icon , and then select**All Stateflow Objects**. From the table at the bottom of the pane, select an input or output argument.In the right pane, enter

`1`

(scaler) for Size, select`Off`

(real number) for**Complexity**, and select`double`

for**Type**.Repeat steps

`2`

and`3`

with the remaining function arguments.

**Set Export Function Parameters for a Graphical Function. **Set parameters to export the graphical function during a simulation.

From the left pane, select the chart containing the graphical function.

Alternatively, open the chart and from the menu, select

**Chart**>**Properties**.In the property dialog box, select

**Export Chart Level Functions (Make Global)**and**Allow exported functions to be called by Simulink**check boxes.If you are calling the exported graphical function from another Stateflow chart (not the chart that exported the graphical function), you do not need to select the

**Allow exported functions****to be called by Simulink**check box.

Set up a MATLAB function in a Stateflow chart to receive data through an input argument from a function caller. Multiply the input argument by 2, and then pass the calculated value back through an output argument. Set chart parameters to export the function to a Simulink model.

**Define MATLAB Function. **Create a graphical function in a Stateflow chart. Define the function interface and function definition.

Add a Stateflow Chart to your Simulink model. From the Simulink Editor menu, select

**File**>**New**>**Chart**. A new chart opens in a new model.Drag the new chart to your model. Double-click the chart to open it.

Add a graphical function. From the left-side toolbar, click and drag the graphical function icon onto the chart.

Define the function interface. In the function box, replace the

`?`

with the function interface`y = timestwo_ml(x)`

.Double-click the function box to open the MATLAB code editor. Define the function with the MATLAB code.

Add a transition to test the MATLAB function. Add a Sine Wave input and connect the output to a Scope.

Run a simulation to test the function.

**Set Argument Parameters. **Specify the size, complexity, and type of the function input and output arguments. A
chart can export only functions with fully specified prototypes.

Open Model Explorer. In the left pane, select the MATLAB function.

In the middle pain, from the

**Column View**list, select`Stateflow`

. From the table, select an input or output argument.In the right pane, enter

`1`

(scaler) for**Size**, select`Off`

(real number) for**Complexity**, and select`double`

for**Type**.Repeat steps

`2`

and`3`

with the remaining function arguments.

**Set Export Function Parameters. **Set parameters to export the MATLAB function during a simulation.

From the left pane, select the chart containing the graphical function.

Alternatively, open the chart and from the menu, select

**Chart**>**Properties**.In the property dialog box, select

**Export Chart Level Function (Make Global)**and**Allow exported functions to be called by Simulink**check boxes.If you are calling the exported graphical function from another Stateflow chart (not the chart that exported the graphical function), you do not need to select the

**Allow exported functions to be called by Simulink**check box.

The following examples show how to use a Function Caller block to call:

Simulink Function block.

Exported Graphical function from a Stateflow chart.

Exported MATLAB function from a Stateflow chart.

The functions multiply a value from the caller by `2`

, and then send
the calculated value back to the caller.

Because the Function Caller block cannot determine function arguments at
compile time, you have to name the Function Caller block input and output
arguments. The argument names in the **Function prototype** must match the
function argument
names.

Set up a Function Caller block to send data through an input argument to a Simulink Function block. Receive data back from the function through an output argument. Points to consider:

The input and output argument names for the Simulink Function block and the prototype for a Function Caller block much match exactly.

If the Function Caller block is within a Model block (referenced model), you must define the

**Input argument specification**and**Output argument specification**. See Simulink Function Blocks in Referenced Models and Argument Specification for Simulink Function Blocks.

To add a Function Caller block.

Add a Function Caller block from the User-Defined Functions library into your model.

In the Function Caller dialog box, in the

**Function prototype**box, enter`y = timestwo(u)`

. This function prototype creates an input port`u`

and output port`y`

on the Function Caller block.### Note

Typing in a blank text box displays a list of previously created function prototypes that match the text you are typing.

Add a Sine Wave block to the input and a Scope block to the output.

Set up a Simulink Function block as described in Simulink Function Block.

**Simulate the Model. **After you create a function using a Simulink Function block and setup
a call to that function using a Function Caller block, you can simulate
the model.

Return to the top level of the model.

Run a simulation.

To view the signal results, double-click the Scope block. The input sine wave with an amplitude of

`2`

is doubled.

Calling an exported graphical function from Stateflow is no different than calling a Simulink Function block. Points to consider:

The input and output argument names for an exported function and the prototype for a Function Caller block much match exactly.

If the Function Caller block is within a Model block (referenced model), you must define the

**Input argument specification**and**Output argument specification**. See Simulink Function Blocks in Referenced Models and Argument Specification for Simulink Function Blocks.

Calling an exported MATLAB function from Stateflow is no different than calling a Simulink Function block. Points to consider:

The input and output argument names for an exported function and the prototype for a Function Caller block must match exactly.

If the Function Caller block is within a Model block (referenced model), you must define the

**Input argument specification**and**Output argument specification**. See Simulink Function Blocks in Referenced Models and Argument Specification for Simulink Function Blocks.

A MATLAB Function block can call another function. The following examples show how to use a MATLAB Function block to call:

Simulink Function block.

Exported Graphical function from a Stateflow chart.

Exported MATLAB function from a Stateflow chart.

The functions multiply a value from the caller by `2`

, and then send
the calculated value back to the caller.

MATLAB Function blocks only support discrete and fixed-in-minor sample times. When using a Sine Wave block as an input signal, setup the model and block parameters for a sample time compatible with the MATLAB Function block:

Discrete — Open the model Configuration Parameters dialog box to the Solver pane. Set

**Type**to Fixed-step and enter a value for**Fixed-step size**(fundamental sample time). Open the Sine Wave Block Parameters dialog box and set**Sample time**to the model sample time you entered.Fixed-In-Minor step — Open the Sine Wave Block Parameters dialog box. In the

**Sample time**box, enter`[0 1]`

.

Set up a MATLAB Function block to send data through an input argument to a Simulink Function block. Receive data back from the function through an output argument.

Add a MATLAB Function block to your model.

Double-click the block, which opens the MATLAB editor. Enter the function call

`y1 = timestwo(u1)`

.### Note

The argument names for the function you define in the MATLAB Function block do not have to match the argument names for the function that you define in the Simulink Function block. For a Function Caller block that calls a Simulink Function block, argument names must match.

Add a Sine Wave block for an input signal and a Scope block to view the output signal.

For the Sine Wave block, set the

**Sample time**to`0.01`

. For the model, open the Configuration Parameters dialog box to the solver pane. Set**Type**to`Fixed-step`

and**Fixed-step size**to`0.01`

.Set up a Simulink Function block as described in Simulink Function Block.

Calling an exported graphical function from a MATLAB Function block is the same as calling a Simulink Function block.

Calling an exported MATLAB function from a MATLAB Function block is the same as calling a Simulink Function block.

The following examples show how to use a Stateflow chart to call functions external to the chart but accessible in a Simulink model:

Simulink Function block

Exported Stateflow graphical function

Exported Stateflow MATLAB function

There are no limitations to a function call from a Stateflow chart:

Caller argument names can be different from the function argument names.

Input signals to a Stateflow chart can be either continuous or discrete.

Add a Stateflow Chart to your Simulink model. From the Simulink Editor menu, select

**File**>**New**>**Chart**. A new chart opens in a new model.Drag the new chart to your model. Double-click the chart to open it.

Add a graphical function. From the left-side toolbar, click and drag the transition icon onto the chart.

Add input port.

From the menu, select

**Chart**>**Add Inputs & Outputs**>**Data Input From Simulink**.In the Data dialog box, set

**Name**to`x1`

.

Add output port.

From the menu, select

**Chart**>**Add Inputs & Outputs**>**Data Output To Simulink**.In the Data dialog box, set

**Name**to`y1`

.

Add a Sine Wave block and connect signal output to the chart input port. Add a Scope block and connect input to the chart output port.

Edit transition code to call a function. For example, to call the Simulink Function block, enter:

{y1=timestwo_sf(x1);}

Set up a Simulink Function block as described in Simulink Function Block.

**Stateflow Chart to Call Exported Graphical Function. **Calling an exported graphical function from a Stateflow chart is the same as calling a Simulink Function
block.

Open a Stateflow chart.

Click transition and enter

{y2=timestwo_gf(x2);}

**Stateflow Chart to Call Exported MATLAB Functions. **Calling an exported MATLAB function from a Stateflow chart is the same as calling a Simulink Function
block.

Open Stateflow chart.

Click transition and enter:

{y3=timestwo_sf(x3);}

If you call a Simulink Function block from multiple sites, all call sites share the state of the function. For example, suppose that you have a Stateflow chart with two calls and two Function Caller blocks with calls to the same function.

A function defined with a Simulink Function block is a counter that
increments by `1`

each time it is called with an input of
`1`

.

The Unit Delay block has state because the block value is persistent between calls from the two Function Caller blocks and the Stateflow chart. Conceptually, you can think of this function being implemented in MATLAB code:

function y = counter(u) persistent state; if isempty(state) state = 0; end y = state; state = state + u;

Simulink initializes the state value of the Unit Delay block at the beginning of a simulation. After that, each time the function is called, the state value is updated.

In this example, the output observed in Scope1 increments by `4`

at
each time step. Scope2, Scope3, and Scope4 show a similar behavior. The only difference is a
shift in the observed signal due to the execution sequence of the function calls.

For multiple callers that share a function and have different sample time rates, data integrity and consistency of real-time code might be a problem. Consider controlling the severity of diagnostics.

Select a **Fixed-step** solver. Set the **Treat each discrete
rate as a separate task** to:

Clear (single–tasking), and then set

**Single task rate transition**parameter to`none`

(default),`warning`

, or`error`

.Select (multi-tasking), and then set

**Multitask rate transition**parameter to`error`

(default) or`warning`

.