MATLAB® functions support MATLAB structures. You can create structures in top-level MATLAB functions to interface with Simulink® bus signals at input and output ports. Simulink buses appear inside the MATLAB function as structures; structure outputs from the MATLAB function appear as buses.
You can also create structures as local and persistent variables in top-level functions and local functions of MATLAB functions.
This section describes how to define structures in MATLAB functions.
Follow these rules when defining structures for MATLAB functions in Stateflow® charts:
For each structure input or output in a MATLAB function, you must define a
Simulink.Bus object in
the base workspace to specify its type to the Simulink signal.
MATLAB structures cannot inherit their type from Simulink signals.
MATLAB functions support nonvirtual buses only (see Types of Composite Signals (Simulink)).
Structures cannot have scopes defined as Constant.
When you create structure inputs in MATLAB functions, the function determines the type, size, and complexity of the structure from the Simulink input signal. When you create structure outputs, you must define their type, size, and complexity in the MATLAB function.
You can connect MATLAB structure inputs and outputs to any Simulink bus signal, including:
Simulink blocks that output bus signals — such as Bus Creator blocks
Simulink blocks that accept bus signals as input — such as Bus Selector and Gain blocks
Other MATLAB functions
To define structure inputs and outputs for MATLAB functions in Stateflow charts, follow these steps:
Create a Simulink bus object in the base workspace to specify the properties of the structure you will create in the MATLAB function.
For information about how to create Simulink bus objects, see Create and Specify Simulink.Bus Objects (Simulink).
Open the Model Explorer and follow these steps:
In the Model Hierarchy pane, select the MATLAB function in your chart.
Add a data object, as described in Add Data Through the Model Explorer.
The Model Explorer adds a data object and opens a Properties dialog box in its right-hand Dialog pane.
In the Properties dialog box, enter the following information in the General tab fields:
|Field||What to Specify|
|Name||Enter a name for referencing the structure in the MATLAB function. This name does not have to match the name of the bus object in the base workspace.|
To add or modify
Simulink.Bus objects, open the Data Type
Assistant. Then, click the Edit button to open the
Simulink Bus Editor (see Create and Specify Simulink.Bus Objects (Simulink)).
If your structure is an output (has scope of
define the output implicitly in the MATLAB function to have the same type, size, and complexity as its
Simulink.Bus object. For details, see Structures (MATLAB Coder).
You can define structures as local or persistent variables inside MATLAB functions. For details, see Structures (MATLAB Coder).