Main Content

Attach Bus Signals to MATLAB Function Blocks

Structures in a MATLAB Function Block

This example shows how to use structures in a MATLAB Function block to read and write to Simulink® bus signals.

In this model, a MATLAB Function block receives a bus signal using the structure inbus at input port 1. The block outputs two bus signals: structure outbus at output port 1 and structure 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 structures outbus and outbus1 connect to Bus Selector blocks that, in turn, connect to Display blocks.

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.

Structure Definitions in Example

Here are the definitions of the structures in the MATLAB Function block in the example, as they appear in the Ports and Data Manager:

This image shows the definitions of the structures in the MATLAB Function block in the example, as they appear in the Ports and Data Manager.

Bus Objects Define Structure Inputs and Outputs

Each structure input and output must be defined by a Simulink.Bus object in the base workspace (see Create 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:

This image shows the display of the bus object properties.

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 Index 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:

StructureFirst FieldSecond FieldThird Field
inbusinbus.ele1inbus.ele2inbus.ele3
outbusoutbus.ele1outbus.ele2outbus.ele3
outbus1outbus1.a1outbus1.a2

Write Buses From Data Store Memory to a MATLAB Function Block

This example shows how to use bus data stored in a data store as an input to a MATLAB Function block.

Capture the Bus Data in a Data Store Memory Block

In this example, the Bus Creator block MainBusCreator creates a bus called MainBus because the Output data type parameter is Bus: MainBus. The Data Store Write block then writes the bus data to a data store named inbus, which the Data store name parameter specifies.

To store the bus data from the Data Store Write block, the model includes a Data Store Memory block. In the Data Store Memory block, the Data store name parameter is inbus, the name of the data store defined by the Data Store Write block. The Data type parameter is Bus: MainBus, the data type specified by the MainBusCreator block.

Define the Data Store Memory Data Argument

To use the data store as an input, the MATLAB Function block uses data that has the Scope property set to Data Store Memory. The function then calls the data by defining the data as a global variable with the name of the data store, which is inbus. Double-click the MATLAB Function block to examine the code.

function [outbus, outbus1] = fcn
global 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;

You can adjust the properties of the data store memory data argument in the Ports and Data Manager or the Model Explorer.

Related Examples

More About