| Simulink® | ![]() |
Embedded MATLAB Function blocks support MATLAB structures (see Structures in the MATLAB Programming Fundamentals documentation). In Embedded MATLAB Function blocks, you can define structure data as inputs or outputs that interact with bus signals. You can also define structures inside Embedded MATLAB functions that are not part of Embedded MATLAB Function blocks (see Using Structures in the Embedded MATLAB documentation).
The following table summarizes how to create different types of structures in Embedded 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 Embedded MATLAB function for interfacing to other environments. See Workflow for Creating Structures in Embedded MATLAB Function Blocks. |
| Output | Create structure data with scope of Output. | |
| Local | Create a local variable implicitly in an Embedded MATLAB function. | See Defining Local Structure Variables in the Embedded MATLAB documentation. |
| Persistent | Declare a variable to be persistent in an Embedded MATLAB function. | See Making Structures Persistent in the Embedded MATLAB documentation. |
| Parameter | Create structure data with scope of Parameter | See Working with Non-Tunable Structure Parameters in Embedded MATLAB Function Blocks. |
Structures in Embedded MATLAB Function blocks can contain fields of any type and size, including muxed signals, buses, and arrays of structures, as described in Elements of Structures in the Embedded MATLAB Subset in the Embedded MATLAB documentation.
The following example shows how to use structures in an Embedded MATLAB Function block:

In this model, an Embedded 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.
Like other outputs in the Embedded MATLAB subset, structure outputs must be initialized. The Embedded MATLAB function in this example 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 Embedded 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 Embedded MATLAB Function Blocks). This means that the structure shares the same properties as the bus object, including number, name, and type 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 Embedded MATLAB, see Workflow for Creating Structures in Embedded MATLAB Function Blocks.
Buses in a Simulink model appear inside the Embedded MATLAB Function block as structures; structure outputs from the Embedded MATLAB Function block appear as buses in Simulink models. When you create structure inputs, the Embedded 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 Embedded MATLAB function.
You connect structure inputs and outputs from Embedded 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 Embedded MATLAB Function blocks
Embedded MATLAB Function blocks supports nonvirtual buses only (see Virtual and Nonvirtual Buses in the Simulink User's Guide). When models that contain Embedded MATLAB function inputs and outputs are built, hidden converter blocks are used to convert bus signals for use with Embedded MATLAB, as follows:
Converts incoming virtual bus signals to nonvirtual buses for Embedded MATLAB structure inputs
Converts outgoing nonvirtual bus signals from Embedded MATLAB to virtual bus signals
Follow these rules when defining structures in Embedded MATLAB Function blocks:
For each structure input or output in an Embedded MATLAB Function block, you must define a Simulink.Bus object in the base workspace to specify its type. For more information, see Simulink.Bus.
Embedded MATLAB Function blocks support nonvirtual buses only (see Working with Virtual and Nonvirtual Buses).
Here is the workflow for creating a structure in Embedded MATLAB:
Decide on the type (or scope) of the structure (see About Structures in Embedded 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 Embedded MATLAB Function Blocks. |
| Output |
|
| Local | Define the structure implicitly as a local variable in the Embedded MATLAB function, as described in Defining Local Structure Variables in the Embedded MATLAB documentation. By default, local variables in Embedded MATLAB are temporary. |
| Persistent | Define the structure implicitly as a persistent variable in the Embedded MATLAB function, as described in Making Structures Persistent in the Embedded MATLAB documentation. |
| Parameter |
See Working with Non-Tunable Structure Parameters in Embedded MATLAB Function Blocks. |
As in MATLAB, you index substructures and fields of Embedded MATLAB structures by using dot notation. Unlike MATLAB, you must reference field values individually (see Limitations with Structures in the Embedded MATLAB documentation).
For example, in the model described in Example of Structures in an Embedded MATLAB Function Block, the Embedded MATLAB function uses dot notation to index fields and substructures:
function [outbus, outbus1] = fcn(inbus)
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 Embedded MATLAB 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 Embedded MATLAB structure, substructure, or field. 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 Embedded 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 an Embedded 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. |
You can define non-tunable structure parameters in Embedded MATLAB Function blocks. Models that contain non-tunable structure parameters will compile both for simulation and code generation with Real-Time Workshop.
To define non-tunable structure parameters in Embedded 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 Embedded 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 | Uncheck this check box |
| Type | Select Inherit: Same as Simulink |
For example, the data dialog box fields should look something like this:

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 Embedded MATLAB Function block. (These FIMATH properties appear in the properties dialog box for Embedded MATLAB Function blocks.)
Non-tunable structure parameters in Embedded MATLAB Function blocks have the same limitations as structures in Embedded MATLAB functions. See Limitations with Structures in the Embedded MATLAB documentation.
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 Embedded 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 Embedded MATLAB Function blocks support a subset of the operations available for MATLAB structures (see Limitations with Structures in the Embedded MATLAB documentation).
![]() | Resolving Signal Objects for Output Data | Working with Frame-Based Signals | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |