When you simulate a Stateflow® chart in a Simulink® model, you can log values for local, output, and active state data into a
Simulink.SimulationData.Dataset
object. After simulation, you can access
this object through the Simulation Data Inspector, the Logic Analyzer, or in the MATLAB® workspace. The workflow for logging data is:
Enable signal logging for the chart and choose a logging format. See Enable Signal Logging.
Configure states and data for signal logging. See Configure States and Data for Logging.
Simulate the chart.
Access the logged data. See Access Signal Logging Data.
Signal logging is enabled by default for models and charts. To disable or reenable signal logging:
Open the Model Configuration Parameters dialog box.
Select Data Import/Export.
In the Signals pane, select the Signal logging check box to enable logging for the chart. To disable logging, clear the check box.
(Optional) Specify a custom name for the signal logging object. The default name
is logsout
. Using this object, you can access the logging data in a
MATLAB workspace variable. For more information, see Export Signal Data Using Signal Logging.
(Optional) In the Format field, select a signal logging format. Options include:
Array
Structure
Structure with time
Dataset
The default setting is Dataset
. For more
information, see Time, State, and Output Data Format.
Click OK.
You can set logging properties for states, local data, and output data from inside the chart, through the Stateflow Signal Logging dialog box, or programmatically from the command line.
Configure logging properties for one state or data object at a time through the Property Inspector, the Model Explorer, or the properties dialog box for the state or data object. Select the Logging tab and modify properties as needed. For more information, see Logging Properties.
For example, in the sf_semantics_hotel_checkin
model:
Open the Hotel
chart.
Open the Symbols pane. In the Simulation tab, in Prepare, click Symbols Pane.
Open the Property Inspector. In the Simulation tab, in Prepare, click Property Inspector.
Configure the service
local data for logging.
In the Symbols pane, select service
.
In the Property Inspector, under Logging, select the Log signal data check box.
Configure the Dining_area
state for logging.
On the Stateflow Editor, select the Dining_area
state.
In the Simulation tab, in Prepare, select Log Self Activity. Alternatively, in the Property Inspector,under Logging, select the Log self activity check box.
By default, the logging name for this state is the hierarchical signal name
Check_in.Checked_in.Executive_suite.Dining_area
. To assign
a shorter name to the state, set Logging Name to
Custom
and enter Dining
Room
.
Configure logging properties for multiple states and data objects through the Stateflow Signal Logging dialog box. Select which chart objects to log from a list of all states, local, and output data. For more information, see Logging Properties.
For example, in the sf_semantics_hotel_checkin
model:
Open the Hotel
chart.
To log multiple signals, press and hold shift to select the states for logging. In the Simulation tab, under Prepare, select Log Self Activity.
The logging badge marks logged signals in the model.
You can add an output port to monitor chart activity. From the Stateflow Editor, in the Simulation tab, click Add Output Port. A new port appears on your Stateflow chart. Connect this port to a viewer to monitor the chart child activity.
Configure logging properties for states and data objects programmatically from the
command line. To enable logging for a states or data object, get a handle for the object
and set its LoggingInfo.DataLogging
property to 1
.
For more information on the Stateflow Programmatic Interface, see Logging.
For example, in the sf_semantics_hotel_checkin
model:
Open the Hotel
chart.
Get a handle for the Dining_area
state:
rt = sfroot; diningState = rt.find('-isa','Stateflow.State','Name','Dining_area');
Get a handle for the local data
service
:
serviceData = rt.find('-isa','Stateflow.Data','Name','service');
Enable logging for the Dining_area
state and the
service
data:
diningState.LoggingInfo.DataLogging = 1; serviceData.LoggingInfo.DataLogging = 1;
Change the logging name of the Dining_area
state to the custom
name Dining Room
:
% Enable custom naming diningState.LoggingInfo.NameMode = 'Custom'; % Enter the custom name diningState.LoggingInfo.LoggingName = 'Dining Room';
During simulation, Stateflow saves logged data in a Simulink.SimulationData.Dataset
signal logging
object.
For example, suppose that you configure the sf_semantics_hotel_checkin
model
to log the service
local data and the activity of the
Dining_area
state. After starting the simulation, you check into the
hotel by toggling the first switch and order room service multiple times by toggling the
second switch. After stopping the simulation, you can view the logged data through the
Simulation Data Inspector, Logic Analyzer, or in the MATLAB workspace.
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, in the Simulation
tab, click the icon .
Inspect and compare the signals logged during simulation. See Simulation Data Inspector.
When you simulate the model, the Logic Analyzer icon is highlighted to indicate that it has new simulation data. To use the Logic Analyzer, you must have DSP System Toolbox™ or SoC Blockset™.
To open the Logic Analyzer, in the Simulation tab, click
the icon .
View, measure and compare the states logged during simulation. See Logic Analyzer (DSP System Toolbox).
To access the signal logging object, at the MATLAB command prompt, enter:
logsout = out.logsout
logsout =
Simulink.SimulationData.Dataset 'logsout' with 2 elements
Name BlockPath
___________ ________________________________
1 [1x1 State] Dining Room sf_semantics_hotel_checkin/Hotel
2 [1x1 Data ] service sf_semantics_hotel_checkin/Hotel
To access logged elements, use the get
method. You can access logged elements by name, index, or block
path.
diningLog = logsout.get('Dining Room')
diningLog =
Stateflow.SimulationData.State
Package: Stateflow.SimulationData
Properties:
Name: 'Dining Room'
BlockPath: [1×1 Simulink.SimulationData.BlockPath]
Values: [1×1 timeseries]
serviceLog = logsout.get('service')
serviceLog =
Stateflow.SimulationData.Data
Package: Stateflow.SimulationData
Properties:
Name: 'service'
BlockPath: [1×1 Simulink.SimulationData.BlockPath]
Values: [1×1 timeseries]
To access the logged data and time of each logged element, use the
Values.Data
and Values.Time
properties.
For example:
Arrange logged data in tabular form by using the table
function.
T1 = table(diningLog.Values.Time,diningLog.Values.Data); T1.Properties.VariableNames = {'Time','Data'}
T1 =
6×2 table
Time Data
__________ ____
0 0
1.8607e+06 1
1.9653e+06 0
1.9653e+06 1
1.9653e+06 0
2.2912e+06 1
T2 = table(serviceLog.Values.Time,serviceLog.Values.Data); T2.Properties.VariableNames = {'Time','Data'}
T2 =
6×2 table
Time Data
__________ ____
1.7076e+06 0
1.8607e+06 1
1.9653e+06 2
1.9653e+06 3
1.9653e+06 4
2.2912e+06 5
View logged data in a figure window by using the plot
function.
X = serviceLog.Values.Time; Y = serviceLog.Values.Data; plot(X,Y,'-o') xlabel('Time') ylabel('Data')
Export logged data to an Excel® spreadsheet by passing an array of logged values to the xlswrite
function:
A = [double(diningLog.Values.Time) double(diningLog.Values.Data)];
xlswrite('dining_log.xls',A);
Stateflow logs each update to a multidimensional signal as a single change. For example,
updating two elements of a matrix A
separately
A[1][1] = 1; A[1][2] = 1;
A
in a single command
A = 1;
A[i][j] = 1
for all values of
i
and j
.When simulating models in external mode, logging of Stateflow data is not supported.
If you log state activity or data from a chart with Fast Restart enabled, any run after the first run duplicates the first logged data points. When you run algorithms that process these data points, you must account for this duplication.
get
| plot
| Simulink.SimulationData.Dataset
| Stateflow.SimulationData.Data
| Stateflow.SimulationData.State
| table
| xlswrite