This example shows how to build a Stateflow® chart that, based on numeric input data, concatenates string data into natural language output text. You can view the output text in the Simulation Data Inspector and in the MATLAB® workspace. For more information on string data, see Manage Textual Information by Using Strings.
During simulation, Sine Wave blocks provide the position of a point
moving along a closed path in terms of latitude and longitude coordinates. The chart
examines these coordinates and assigns the strings q1
and
q2
according to the information in this table.
Latitude | Longitude | q1 | q2 |
---|---|---|---|
positive | positive | "North" | "east" |
positive | negative | "North" | "west" |
negative | positive | "South" | "east" |
negative | negative | "South" | "west" |
Then, the statement
sout = strcat("Location in ",q1,q2," Quadrant");
To create a Simulink® model with an empty chart that uses the C action language, at the MATLAB command prompt, enter
sfnew -c
In the empty chart, place a default transition. A junction appears. Point and drag from the edge of the junction to add other transitions and junctions.
Double-click each transition. At the text prompt, enter the appropriate condition or action statement.
To resolve the undefined data, in the Symbols pane, click the
Resolve undefined symbols icon . The Stateflow Editor assigns an appropriate scope to each symbol in the
chart.
Symbol | Scope |
---|---|
latitude | Input Data |
longitude | Input Data |
q1 | Local Data |
q2 | Local Data |
sout | Output Data |
To specify q1
as string data, in the
Type field of the Property Inspector, select
string
. Repeat that specification for
q2
and sout
.
Alternatively, to create string data with a maximum number of
characters, specify each string as
stringtype(n)
using a
suitable buffer size n
to avoid
truncation of its contents. For instance, this table lists suitable
buffer sizes for the string data in the chart.
Symbol | Number of Characters | String Data Type |
---|---|---|
q1 | 5 | stringtype(5) |
q2 | 5 | stringtype(5) |
sout | 30 | stringtype(30) |
In the Simulink model, add two Sine Wave blocks and a Display block. Connect the blocks to the chart input and output ports.
Set the Sine Wave block parameters as indicated in this table.
Block | Amplitude | Bias | Frequency | Phase |
---|---|---|---|---|
Latitude | 50 | 0 | 1 | 0 |
Longitude | 50 | 0 | 1 | pi/2 |
Label the signals in the model as latitude
,
longitude
, and sout
.
Right-click each signal and select Log Selected
Signals.
When you simulate the model, the Simulation Data
Inspector icon is highlighted to indicate that it has new
simulation data. To open the Simulation Data Inspector, click the
icon.
In the Simulation Data Inspector, select the check boxes for the
latitude
, longitude
, and
sout
signals so that they are displayed on the
same set of axes. The latitude
and
longitude
signals appear as sinusoidal curves.
The sout
signal is shown as a transition plot. The
value of the string is displayed inside a band and criss-crossed lines
mark the changes in value.
To access the logged data in the MATLAB workspace, call the signal logging object
logsout
. Stateflow exports the string data sout
as a
MATLAB string scalar. For example, at the command prompt,
enter:
losgout = out.logsout; Tbl = table(logsout.get('latitude').Values.Data, ... logsout.get('longitude').Values.Data, ... logsout.get('sout').Values.Data); Tbl.Properties.VariableNames = ... {'Latitude','Longitude','QuadrantInfo'}; Tbl([4:8:30],:)
ans =
4×3 table
Latitude Longitude QuadrantInfo
________ _________ ________________________________
28.232 41.267 "Location in Northeast Quadrant"
40.425 -29.425 "Location in Northwest Quadrant"
-30.593 -39.548 "Location in Southwest Quadrant"
-38.638 31.735 "Location in Southeast Quadrant"