| Contents | Index |
MATLAB Function blocks support MATLAB structures (see Structures in the MATLAB getting started guide). In MATLAB Function blocks, you can define structure data as inputs or outputs that interact with bus signals. MATLAB Function blocks also support arrays of buses (for more information, see Combining Buses into an Array of Buses). You can also define structures inside MATLAB functions that are not part of MATLAB Function blocks (see Code Generation for MATLAB Structuresin the Code Generation from MATLAB documentation).
The following table summarizes how to create different types of structures in MATLAB Function blocks:
| Scope | How to Create | Details |
|---|---|---|
| Input | Create structure data with scope of Input. | You can create structure data as inputs or outputs in the top-level MATLAB function for interfacing to other environments. See Workflow for Creating Structures in MATLAB Function Blocks. |
| Output | Create structure data with scope of Output. | |
| Local | Create a local variable implicitly in a MATLAB function. | See Defining Scalar Structures for Code Generation in the Code Generation from MATLAB documentation. |
| Persistent | Declare a variable to be persistent in aa MATLAB function. | See Making Structures Persistent in the Code Generation from MATLAB documentation. |
| Parameter | Create structure data with scope of Parameter | See Working with Structure Parameters in MATLAB Function Blocks. |
Structures in MATLAB Function blocks can contain fields of any type and size, including muxed signals, buses and arrays of structures.
For an example of how to use structures in a MATLAB Function block, open the model emldemo_bus_struct.
In this model, a MATLAB Function block receives a bus signal using the structure inbus at input port 1 and outputs two bus signals from the structures outbus at output port 1 and outbus1 at output port 2. The input signal comes from the Bus Creator block MainBusCreator, which bundles signals ele1, ele2, and ele3. The signal ele3 is the output of another Bus Creator block SubBusCreator, which bundles the signals a1 and a2. The structure outbus connects to a Bus Selector block BusSelector1; the structure outbus1 connects to another Bus Selector block BusSelector3.
To explore the MATLAB function fcn, double-click the MATLAB Function block. Notice that the code implicitly defines a local structure variable mystruct using the struct function, and uses this local structure variable to initialize the value of the first output outbus. It initializes the second output outbus1 to the value of field ele3 of structure inbus.
Here are the definitions of the structures in the MATLAB Function block in the example, as they appear in the Ports and Data Manager:

Each structure input and output must be defined by a Simulink.Bus object in the base workspace (see Workflow for Creating Structures in MATLAB Function Blocks). This means that the structure shares the same properties as the bus object, including number, name, type, and sequence of fields. In this example, the following bus objects define the structure inputs and outputs:

The Simulink.Bus object MainBus defines structure input inbus and structure output outbus. The Simulink.Bus object SubBus defines structure output outbus1. Based on these definitions, inbus and outbus have the same properties as MainBus and, therefore, reference their fields by the same names as the fields in MainBus, using dot notation (see Indexing Substructures and Fields). Similarly, outbus1 references its fields by the same names as the fields in SubBus. Here are the field references for each structure in this example:
| Structure | First Field | Second Field | Third Field |
|---|---|---|---|
| inbus | inbus.ele1 | inbus.ele2 | inbus.ele3 |
| outbus | outbus.ele1 | outbus.ele2 | outbus.ele3 |
| outbus1 | outbus1.a1 | outbus1.a2 | — |
To learn how to define structures in MATLAB Function blocks, see Workflow for Creating Structures in MATLAB Function Blocks.
Buses in a Simulink model appear inside the MATLAB Function block as structures; structure outputs from the MATLAB Function block appear as buses in Simulink models. When you create structure inputs, the MATLAB Function block determines the type, size, and complexity of the structure from the input signal. When you create structure outputs, you must define their type, size, and complexity in the MATLAB function.
You connect structure inputs and outputs from MATLAB Function blocks to any bus signal, including:
Blocks that output bus signals — such as Bus Creator blocks
Blocks that accept bus signals as input — such as Bus Selector and Gain blocks
S-Function blocks
Other MATLAB Function blocks
MATLAB Function blocks supports nonvirtual buses only (see Virtual and Nonvirtual Buses in the Simulink User's Guide). When models that contain MATLAB Function block inputs and outputs are built, hidden converter blocks are used to convert bus signals for code generation from MATLAB, as follows:
Converts incoming virtual bus signals to nonvirtual buses for inputs to structures in MATLAB Function blocks
Converts outgoing nonvirtual bus signals from MATLAB Function blocks to virtual bus signals
Follow these rules when defining structures in MATLAB Function blocks:
For each structure input or output in a MATLAB Function block, you must define a Simulink.Bus object in the base workspace to specify its type. For more information, see Simulink.Bus.
MATLAB Function blocks support nonvirtual buses only (see Working with Virtual and Nonvirtual Buses).
Here is the workflow for creating a structure in a MATLAB Function block:
Decide on the type (or scope) of the structure (see About Structures in MATLAB Function Blocks).
Based on the scope, follow these guidelines for creating the structure:
| For Structure Scope: | Follow These Steps: |
|---|---|
| Input |
See Rules for Defining Structures in MATLAB Function Blocks. |
| Output |
|
| Local | Define the structure implicitly as a local variable in the MATLAB function, as described in Code Generation for MATLAB Structures in the Code Generation from MATLAB documentation. By default, local variables in MATLAB Function blocks are temporary. |
| Persistent | Define the structure implicitly as a persistent variable in the MATLAB function, as described in Making Structures Persistent in the Code Generation from MATLAB documentation. |
| Parameter |
See Working with Structure Parameters in MATLAB Function Blocks. |
As in MATLAB, you index substructures and fields structures in MATLAB Function blocks by using dot notation. However, for code generation from MATLAB, you must reference field values individually (see How Working with Structures Is Different for Code Generation in the Code Generation from MATLAB documentation).
For example, in the emldemo_bus_struct model described in Example of Structures in a MATLAB Function Block, the MATLAB function uses dot notation to index fields and substructures:
function [outbus, outbus1] = fcn(inbus)
%#codegen
substruct.a1 = inbus.ele3.a1;
substruct.a2 = int8([1 2;3 4]);
mystruct = struct('ele1',20.5,'ele2',single(100),
'ele3',substruct);
outbus = mystruct;
outbus.ele3.a2 = 2*(substruct.a2);
outbus1 = inbus.ele3;The following table shows how the code generation software resolves symbols in dot notation for indexing elements of the structures in this example:
| Dot Notation | Symbol Resolution |
|---|---|
| substruct.a1 | Field a1 of local structure substruct |
| inbus.ele3.a1 | Value of field a1 of field ele3, a substructure of structure inputinbus |
| inbus.ele3.a2(1,1) | Value in row 1, column 1 of field a2 of field ele3, a substructure of structure input inbus |
You can assign values to any structure, substructure, or field in a MATLAB Function block. Here are the guidelines:
| Operation | Conditions |
|---|---|
| Assign one structure to another structure | You must define each structure with the same number, type, and size of fields, either as Simulink.Bus objects in the base workspace or locally as implicit structure declarations (see Workflow for Creating Structures in MATLAB Function Blocks). |
| Assign one structure to a substructure of a different structure and vice versa | You must define the structure with the same number, type, and size of fields as the substructure, either as Simulink.Bus objects in the base workspace or locally as implicit structure declarations. |
| Assign an element of one structure to an element of another structure | The elements must have the same type and size. |
For example, the following table presents valid and invalid structure assignments based on the specifications for the model described in Example of Structures in a MATLAB Function Block:
| Assignment | Valid or Invalid? | Rationale |
|---|---|---|
| outbus = mystruct; | Valid | Both outbus and mystruct have the same number, type, and size of fields. The structure outbus is defined by the Simulink.Bus object MainBus and mystruct is defined locally to match the field properties of MainBus. |
| outbus = inbus; | Valid | Both outbus and inbus are defined by the same Simulink.Bus object, MainBus. |
| outbus1 = inbus.ele3; | Valid | Both outbus1 and inbus.ele3 have the same type and size because each is defined by the Simulink.Bus object SubBus. |
| outbus1 = inbus; | Invalid | The structure outbus1 is defined by a different Simulink.Bus object than the structure inbus. |
To define structure parameters in MATLAB Function blocks, follow these steps:
Define and initialize a structure variable
A common method is to create a structure in the base workspace. For other methods, see Working with Block Parameters.
In the Ports and Data Manager or Model Explorer, add data in the MATLAB Function block with the following properties:
| Property | What to Specify |
|---|---|
| Name | Enter same name as the structure variable you defined in the base workspace |
| Scope | Select Parameter |
| Tunable | Leave checked if you want to change (tune) the value of the parameter during simulation; otherwise, clear to make the parameter non-tunable and preserve the initial value during simulation |
| Type | Select Inherit: Same as Simulink |
Click Apply.
FIMATH properties for non-tunable structure parameters containing fixed-point values are based on the initial values of the structure. They do not come from the FIMATH properties specified for fixed-point input signals to the parent MATLAB Function block. (These FIMATH properties appear in the properties dialog box for MATLAB Function blocks.)
The following simple example uses a non-tunable structure parameter input to initialize a matrix output. The model looks like this:

This model defines a structure variable p in its pre-load callback function, as follows:

The structure p has two fields, rows and cols, which specify the dimensions of a matrix. The MATLAB Function block uses a constant input u to initialize the matrix output y. Here is the code:
function y = fcn(u, p) y = zeros(p.rows,p.cols) + u;
Running the model initializes each element of the 2-by-3 matrix y to 99, the value of u:

Structures in MATLAB Function blocks support a subset of the operations available for MATLAB structures (see How Working with Structures Is Different for Code Generation in the Code Generation from MATLAB documentation).
You cannot use variable-size data with arrays of buses (see Array of Buses Limitations).
![]() | Resolving Signal Objects for Output Data | Using Variable-Size Data in MATLAB Function Blocks | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |