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® structures. 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 structure

  • 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 structures in and out. The function sb2abc extracts information from the input structure and stores it in the local Stateflow structure localbus. Then the chart writes to the output structure by combining the values of the local structure and one of the elements of the array of structures subBusArray. For more information on accessing and modifying the contents of a Stateflow structure or an array of Stateflow structures, see Index and Assign Values to Stateflow Structures.

The MATLAB® function sb2abc takes a Stateflow structure of type SubBus and returns a Stateflow structure 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 structure y.

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

Define Input and Output Structures

In a MATLAB function, you can access a local Stateflow structure or interface with a Simulink bus signal by defining the input and output structures for the function:

  1. In the base workspace, create a Simulink.Bus object that defines the structure 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 structure.

For example, in the function sb2abc:

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

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

For more information, see Define Stateflow Structures.

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 structure u before writing to the output structure 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 structure 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 structure of the Simulink.Bus object BusObject.

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

See Also

| (Simulink)

Related Topics