Access Simulink Bus Signals in MATLAB Functions - MATLAB & Simulink (original) (raw)

Main Content

This example shows how to read from and write to Simulink® bus signals in a MATLAB® function by using MATLAB and Stateflow® buses. MATLAB structures enable you to bundle data of different sizes and types into a single variable. You can create a MATLAB structure to:

MATLAB functions support nonvirtual buses only. For more information, see Composite Interface Guidelines (Simulink).

Define Structures in MATLAB Functions

In this example, a Stateflow chart processes data from one Simulink bus signal and outputs the result to another Simulink bus signal. Both the input and output bus signals are defined by the Simulink.Bus (Simulink) object BusObject. These buses have four fields: sb, a, b, and c. The field sb is also a bus signal defined by the Simulink.Bus object SubBus. It has one field called ele.

In the chart, the Simulink bus signals interface with the Stateflow buses in and out. The function sb2abc extracts information from the input bus and stores it in the local Stateflow bus localbus. Then the chart writes to the output bus by combining the values of the local bus and one of the elements of the array of structures subBusArray. For more information on accessing and modifying the contents of a Stateflow bus or an array of Stateflow buses, see Index and Assign Values to Stateflow Structures.

The MATLAB® function sb2abc takes a Stateflow bus of type SubBus and returns a Stateflow bus of type BusObject. The function decomposes the value of the field ele from its input into three components: a vector, a 3-by-2 matrix, and a scalar. The function stores these components in a local MATLAB [struct](../../matlab/ref/struct.html) that has the same fields as the Simulink.Bus object BusObject. Then the function assigns the value of the MATLAB struct to the output bus y.

% extract data from input bus

A = double(u.ele(1:2,1)); B = uint8(u.ele(:,2:3)); C = double(u.ele(3,1));

X = struct(ele=int8(zeros(3))); Y = struct(sb=X,a=A,b=B,c=C);

% assign value to output bus

Define Input and Output Structures

To access a local Stateflow bus or interface with a Simulink bus signal in a MATLAB function, define the input and output buses for the function:

  1. In the Simulink base workspace, create a Simulink.Bus object that defines the bus data type.
  2. In the Symbols pane, select the function input or output.
  3. In the Property Inspector, set the Type property to Bus: <object name>. Replace with the name of the Simulink.Bus object that defines the Stateflow bus.

For example, in the function sb2abc:

For more information, see Define Stateflow Buses.

Define Local and Persistent Structure Variables

To store related data in a single variable inside a MATLAB function, you can create a MATLAB struct as a local or persistent variable. For example, the function sb2abc defines two local MATLAB structures to temporarily store the data extracted from the input bus u before writing to the output bus y:

For more information, see Define Scalar Structures for Code Generation (Simulink).

See Also

struct | Simulink.Bus (Simulink)

Topics