Log and View Data with the Stateflow API
This example shows how to log and view logged data from the command line.
Logging data can help you debug issues with chart design or analyze data from your simulation. By using the command-line API, you can analyze large sets of data by creating scripts that log data, display the data in MATLAB, or export the data to Excel. For more information on the Stateflow application programming interface, see Overview of the Stateflow API.
Log Chart Signals
To enable logging for a state or data object, get a handle to the object and set its LoggingInfo.DataLogging
property to true
.
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");
View Logged Data in the MATLAB Workspace
To access the data logged during simulation, use 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: [1×1 Simulink.SimulationData.BlockPath] Values: [1×1 timeseries] Methods, Superclasses
reelStateLog = get(logsout,"ReelState")
reelStateLog = Stateflow.SimulationData.Data Package: Stateflow.SimulationData Properties: Name: 'ReelState' BlockPath: [1×1 Simulink.SimulationData.BlockPath] Values: [1×1 timeseries] Methods, Superclasses
To access the logged data and time of each logged element, use the Values.Data
and Values.Time
properties. 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])
The signal logging object records a data point every time that the Stateflow chart writes to the data, even if the data does not change value. For example, 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
.
See Also
Objects
Simulink.SimulationData.Dataset
(Simulink) |Stateflow.SimulationData.Data
|Stateflow.SimulationData.State
Functions
Tools
- Simulation Data Inspector (Simulink) | Signal Properties (Simulink)