This example shows how to use MATLAB® structures in a Stateflow® chart. MATLAB structures enable you to bundle data of different sizes and types together into a single variable in a MATLAB function. 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 Types of Composite Signals.
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
BusObject. These buses have four fields:
c. The field
sb is also a bus signal defined by the
SubBus. It has one field called
In the chart, the Simulink bus signals interface with the Stateflow structures
out. The functions
sb2abc extract information from the input structure and store it in the local Stateflow structures
subbus. Then, the chart writes to the output structure by combining the values of these local structures. For more information on Stateflow structures, see Access Bus Signals Through 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 the local MATLAB
Y that has the same structure as the
BusObject. Then, the function assigns the value of the MATLAB
Y to the output structure
function y = sb2abc(u)
% extract data from input structure
E = int8(magic(3)); 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',E); Y = struct('sb',X,'a',A,'b',B,'c',C);
% assign value to output structure
y = Y; end
In a MATLAB function, access a local Stateflow structure or interface with a Simulink bus signal by defining input and output structures for the function:
In the base workspace, create a
Simulink.Bus object that defines the structure data type.
In the Symbols pane, select the function input or output.
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
The Type property of the input structure
u is specified as
The Type property of the output structure
y is specified as
For more information, see Define Stateflow Structures.
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
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
Y is a scalar
struct with four fields:
sb is a substructure of type
a is a two-dimensional vector of type
b is a 3-by-2 matrix of type
c is a scalar of type
double. These fields match the structure of the
For more information, see Define Scalar Structures for Code Generation.