Log Simulation Output for States and Data
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
Signal logging is enabled by default for models and charts. To disable or reenable signal logging:
Open the Configuration Parameters dialog box.
In the Data Import/Export pane, select Signal logging to enable logging for the chart. To disable logging, clear Signal logging. For more information, see Signal logging (Simulink).
(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 (Simulink).
(Optional) In the Format field, select a signal logging format. Options include:
Structure with time
The default setting is
Dataset. For more information, see Time, State, and Output Data Format (Simulink).
Configure States and Data for Logging
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.
Log Individual States and Data
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 more information about this example, see How Stateflow Objects Interact During Execution.
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.
servicelocal data for logging.
In the Symbols pane, select
In the Property Inspector, under Logging, select the Log signal data check box.
Dining_areastate for logging.
On the Stateflow Editor, select the
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
Log Multiple Signals
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.
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.
Add an Output Port
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.
Log Chart Signals by Using the Command-Line API
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
1. For more information on the Stateflow Programmatic Interface, see Overview of the Stateflow API.
Stateflow.Stateobject that corresponds to the
diningState = find(sfroot,"-isa","Stateflow.State",Name="Dining_area");
Stateflow.Dataobject that corresponds to the local data
serviceData = find(sfroot,"-isa","Stateflow.Data",Name="service");
Enable logging for the
Dining_areastate and the
diningState.LoggingInfo.DataLogging = true; serviceData.LoggingInfo.DataLogging = true;
Change the logging name of the
Dining_areastate to the custom name
% Enable custom naming diningState.LoggingInfo.NameMode = "Custom"; % Enter the custom name diningState.LoggingInfo.LoggingName = "Dining Room";
Access Signal Logging Data
During simulation, Stateflow saves logged data in a
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 twice and order room service by toggling
the second switch multiple times. After stopping the simulation, you can view the
logged data through the Simulation Data Inspector, Logic Analyzer, or in the
View Logged Data Through the Simulation Data Inspector
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 (Simulink).
View Logged Data Through the Logic Analyzer
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).
View Logged Data in the MATLAB Workspace
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
getfunction. You can access logged elements by name, index, or block path.
diningLog = get(logsout,"Dining Room")
diningLog = Stateflow.SimulationData.State Package: Stateflow.SimulationData Properties: Name: 'Dining Room' BlockPath: [1×1 Simulink.SimulationData.BlockPath] Values: [1×1 timeseries]
serviceLog = get(logsout,"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.Timeproperties. For example:
Arrange logged data in tabular form by using the
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 __________ ____ 0 0 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
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
A = [double(diningLog.Values.Time) double(diningLog.Values.Data)]; xlswrite("dining_log.xls",A);
The signal logging object records a data point every time that the
Stateflow chart writes to the data you are logging, even if the data
does not change value. For instance, in this example, the data points with
0 correspond to when the chart initializes the
0 at time
0 and when a default transition sets
0 at time
Log Multidimensional Data
Stateflow logs each update to a multidimensional signal as a single change. For
example, updating two elements of a matrix
A = 1; A = 1;
Ain a single command
A = 1;
A[i][j] = 1for all values of
Limitations on Logging Data
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.
- Signal Properties (Simulink)