Log Simulation Output for States and Data
Logging the simulation output of a Stateflow® chart helps you understand when states execute and how properties change over time.
You can log values for local, output, and active state data into a Simulink.SimulationData.Dataset
object. After you simulate the Simulink® model, you can access this object through the Simulation Data Inspector (Simulink), the Logic Analyzer (DSP System Toolbox), or in the MATLAB® workspace.
To log simulation data:
Enable signal logging for the chart and choose a logging format.
Configure states and data for signal logging.
Simulate the chart.
Access the logged data.
In this example, you log the output of the control system of a tethered satellite. For information on how the model operates, see Yo-Yo Control of Satellites.
Enable and Customize Signal Logging
Signal logging is enabled by default for models and charts. To enable, disable, or customize signal logging:
Open the Configuration Parameters dialog box.
In the Data Import/Export pane, select or clear Signal logging. For more information, see Signal logging (Simulink).
Optionally, specify a custom name for the signal logging object. The default name is
logsout
. This parameter specifies the name of the MATLAB workspace variable that stores the logging data. For more information, see Save Signal Data Using Signal Logging (Simulink).Optionally, select a signal logging format using the Format parameter. The default format 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 or programmatically from the command line.
Log Individual States and Data
You can configure logging properties for one state or data object at a time by using 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:
Open the chart.
Open the Symbols pane. In the Simulation tab, in the Prepare section, click Symbols Pane.
Open the Property Inspector. In the Simulation tab, in the Prepare section, click Property Inspector.
In the Symbols pane, select
ReelState
.In the Property Inspector, under Logging, select the Log signal data check box.
On the Stateflow Editor, select the
ReelMoving
state.In the Simulation tab, in the Prepare section, click the arrow on the right side of the section and 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
Active.ReelMoving
. To assign a shorter name, set Logging Name toCustom
and enterReelMoving
.
Log Multiple Signals
You can configure logging properties for multiple states and data objects with the Simulink toolstrip. For more information, see Logging Properties.
For example:
Open the chart.
You can select multiple states by holding shift while clicking. Select the
ReelIn
andReelOut
states.In the Simulation tab, under Prepare, click the arrow on the right side of the section and select Log Self Activity.
The logging badge marks logged signals in the model.
Monitor Data by Adding an Output Port
You can add an output port to monitor chart activity. For example:
In the Simulation tab, click Add Output Port. A new port titled
ChartMode
appears on the chart in Simulink.Go to the Simulink Editor.
From the Simulink Sinks library, add a Scope (Simulink) block.
Connect the new output port to the Scope block.
Log Chart Signals by Using the Command-Line API
You can configure logging properties for states and data objects programmatically from the command line. To enable logging for a state or data object, get a handle to the object and set its LoggingInfo.DataLogging
property to 1
. For example:
1. Load the model:
load_system("sf_yoyo");
2. Access the Stateflow.State
object that corresponds to the state ReelMoving
:
reelMoving = find(sfroot,"-isa","Stateflow.State", ... Name="ReelMoving");
3. Access the Stateflow.Data
object that corresponds to the output data ReelState
:
reelState = find(sfroot,"-isa","Stateflow.Data", ... Name="ReelState");
4. Enable logging for the state ReelMoving
and the output data ReelState
:
reelMoving.LoggingInfo.DataLogging = true; reelState.LoggingInfo.DataLogging = true;
5. Enable custom naming and change the logging name of the state ReelMoving
to Reel Moving Log
:
reelMoving.LoggingInfo.NameMode = "Custom"; reelMoving.LoggingInfo.LoggingName = "Reel Moving Log";
6. Run the model:
sim("sf_yoyo.slx");
For more information on the Stateflow application programming interface, see Overview of the Stateflow API.
Access Signal Logging Data
During simulation, Stateflow saves logged data in a Simulink.SimulationData.Dataset
signal logging object.
After running a simulation, you can view the logged data in the Simulation Data Inspector, Logic Analyzer, or in the MATLAB workspace.
View Logged Data Through the Simulation Data Inspector
You can use the Simulation Data Inspector to view and compare data across simulations. To open the Simulation Data Inspector, in the Simulation tab, click Data Inspector. When you simulate the model, the Simulation Data Inspector icon highlights to indicate new simulation data. For more information, see View State Activity by Using the Simulation Data Inspector.
View Logged Data Through the Logic Analyzer
You can use the Logic Analyzer to visualize and measure transitions, signals, and states over time. To open the Logic Analyzer, in the Simulation tab, select Logic Analyzer. When you simulate the model, the icon is highlighted to indicate that the Logic Analyzer has new simulation data. For more information, see View Stateflow States in the Logic Analyzer.
To use the Logic Analyzer, you must have DSP System Toolbox™, SoC Blockset™, or HDL Verifier™.
View Logged Data in the MATLAB Workspace
You can programmatically access logged data to display the data in MATLAB, export the data to a plot, or export the data to Excel. First, access the logged elements by using the get
function. For example, get the data for the ReelMoving
state and the ReelState
output data by entering:
reelMovingLog = get(logsout,"Reel Moving Log")
reelMovingLog = Stateflow.SimulationData.State Package: Stateflow.SimulationData Properties: Name: 'Reel Moving Log' BlockPath: [1x1 Simulink.SimulationData.BlockPath] Values: [1x1 timeseries]
reelStateLog = get(logsout,"ReelState")
reelStateLog = Stateflow.SimulationData.Data Package: Stateflow.SimulationData Properties: Name: 'ReelState' BlockPath: [1x1 Simulink.SimulationData.BlockPath] Values: [1x1 timeseries]
To access the logged data and time of each logged element, use the Values.Data
and Values.Time
properties. For example, arrange the logged data as a table by entering:
T1 = table(reelMovingLog.Values.Time,reelMovingLog.Values.Data); T1.Properties.VariableNames = ["Time","Data"]
T1=121×2 table
Time Data
______ ____
0 0
5.025 1
5.275 0
10.16 1
10.66 0
15.03 1
15.53 0
20.124 1
20.624 0
24.992 1
25.492 0
30.084 1
30.584 0
34.951 1
35.451 0
40.041 1
⋮
T2 = table(reelStateLog.Values.Time,reelStateLog.Values.Data); T2.Properties.VariableNames = ["Time","Data"]
T2=122×2 table
Time Data
__________ ____
0 0
3.1554e-30 0
5.025 1
5.275 0
10.16 -1
10.66 0
15.03 1
15.53 0
20.124 -1
20.624 0
24.992 1
25.492 0
30.084 -1
30.584 0
34.951 1
35.451 0
⋮
View the logged data in a figure window by using the plot
function:
X = reelStateLog.Values.Time; Y = reelStateLog.Values.Data; plot(X,Y,"-o") xlabel("Time") ylabel("Data") set(gcf,"position",[100,100,1000,400])
Export logged data to an Excel® spreadsheet by passing an array of logged values to the xlswrite
function:
time = double(reelMovingLog.Values.Time);
data = double(reelMovingLog.Values.Data);
logs = [time data];
writematrix(logs,"reel_moving_log.xls");
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 the table T2
, the first two entries contain a value of 0
. These entries correspond to when the chart initializes the output data ReelState
at time 0
and when a default transition sets ReelState
at time 3.1554e-30
.
Log Multidimensional Data
Stateflow logs each update to a multidimensional signal as a single change. For example, updating two elements of a matrix A separately produces two different changes in the logged data:
A[1][1] = 1; A[1][2] = 1;
In contrast, updating a matrix A in a single command produces a single change in the logged data, even though the command implies A[i][j] = 1
for all values of i
and j
:
A = 1;
Limitations on Logging Data
When simulating models in external mode, logging 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.
See Also
Apps
- Logic Analyzer (DSP System Toolbox)
Objects
Simulink.SimulationData.Dataset
(Simulink) |Stateflow.SimulationData.Data
|Stateflow.SimulationData.State
Functions
Tools
- Simulation Data Inspector (Simulink) | Signal Properties (Simulink)
Related Topics
- Save Signal Data Using Signal Logging (Simulink)