Main Content

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:

  1. Enable signal logging for the chart and choose a logging format.

  2. Configure states and data for signal logging.

  3. Simulate the chart.

  4. 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.

log_simulation_output_sfyoyo_fullchart.png

Enable and Customize Signal Logging

Signal logging is enabled by default for models and charts. To enable, disable, or customize signal logging:

  1. Open the Configuration Parameters dialog box.

  2. In the Data Import/Export pane, select or clear Signal logging. For more information, see Signal logging (Simulink).

  3. 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).

  4. 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:

  1. Open the chart.

  2. Open the Symbols pane. In the Simulation tab, in the Prepare section, click Symbols Pane.

  3. Open the Property Inspector. In the Simulation tab, in the Prepare section, click Property Inspector.

  4. In the Symbols pane, select ReelState.

  5. In the Property Inspector, under Logging, select the Log signal data check box.

  6. On the Stateflow Editor, select the ReelMoving state.

  7. 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.

  8. By default, the logging name for this state is Active.ReelMoving. To assign a shorter name, set Logging Name to Custom and enter ReelMoving.

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:

  1. Open the chart.

  2. You can select multiple states by holding shift while clicking. Select the ReelIn and ReelOut states.

  3. In the Simulation tab, under Prepare, click the arrow on the right side of the section and select Log Self Activity.

  4. The logging badge marks logged signals in the model.

log_simulation_output_states_with_badges.png

Monitor Data by Adding an Output Port

You can add an output port to monitor chart activity. For example:

  1. In the Simulation tab, click Add Output Port. A new port titled ChartMode appears on the chart in Simulink.

  2. Go to the Simulink Editor.

  3. From the Simulink Sinks library, add a Scope (Simulink) block.

  4. Connect the new output port to the Scope block.

log_simulation_output_add_output_port.png

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.

log_simulation_output_simulation_data_inspector.png

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™.

log_simulation_output_logic_analyzer.png

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])

Figure contains an axes object. The axes object with xlabel Time, ylabel Data contains an object of type line.

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

Objects

Functions

Tools

Related Topics