Main Content

Access Simulink Bus Signals in MATLAB Functions

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:

  • Store related data in a local or persistent variable of a MATLAB function

  • Read from or write to a local Stateflow bus

  • Interface with a Simulink bus signal at an input or output port

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 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.

  • X is a scalar struct with a single field called ele. This field contains a 3-by-3 matrix of type int8, which matches the layout of the Simulink.Bus object SubBus.

  • Y is a scalar struct with four fields: sb is a substructure of type SubBus, a is a two-dimensional vector of type double, b is a 3-by-2 matrix of type uint8, and c is a scalar of type double. These fields match the layout of the Simulink.Bus object BusObject.

function y = sb2abc(u)
% extract data from input bus
A = double(u.ele(1:2,1));
B = uint8(u.ele(:,2:3));
C = double(u.ele(3,1));
% create local bus
X = struct(ele=int8(zeros(3)));
Y = struct(sb=X,a=A,b=B,c=C);
% assign value to output bus
y = Y;
end

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 bused 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 <object name> with the name of the Simulink.Bus object that defines the Stateflow bus.

For example, in the function sb2abc:

  • The Type property of the input u is specified as Bus: SubBus.

  • The Type property of the output y is specified as Bus: BusObject.

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

| (Simulink)

Related Topics