Main Content

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 (Simulink), the Logic Analyzer (DSP System Toolbox), or in the MATLAB® workspace. The workflow for logging data is:

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

  2. Configure states and data for signal logging. See Configure States and Data for Logging.

  3. Simulate the chart.

  4. Access the logged data. See Access Signal Logging Data.

Enable Signal Logging

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

  1. Open the Configuration Parameters dialog box.

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

  3. (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 Save Signal Data Using Signal Logging (Simulink).

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

  1. Open the sf_semantics_hotel_checkin model:

    openExample("sf_semantics_hotel_checkin")

    For more information about this example, see How Stateflow Objects Interact During Execution.

  2. Open the Hotel chart.

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

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

  5. Configure the service local data for logging.

    1. In the Symbols pane, select service.

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

  6. Configure the Dining_area state for logging.

    1. On the Stateflow Editor, select the Dining_area state.

    2. In the Simulation tab, in Prepare, select Log Self Activity. Alternatively, in the Property Inspector,under Logging, select the Log self activity check box.

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

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.

For example:

  1. Open the sf_semantics_hotel_checkin model:

    openExample("sf_semantics_hotel_checkin")
  2. Open the Hotel chart.

  3. To log multiple signals, press and hold shift to select the states for logging. In the Simulation tab, under Prepare, select Log Self Activity.

  4. 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 LoggingInfo.DataLogging property to 1. For more information on the Stateflow Programmatic Interface, see Overview of the Stateflow API.

For example:

  1. Open the sf_semantics_hotel_checkin model:

    openExample("sf_semantics_hotel_checkin")
  2. Access the Stateflow.State object that corresponds to the Dining_area state:

    diningState = find(sfroot,"-isa","Stateflow.State",Name="Dining_area");

  3. Access the Stateflow.Data object that corresponds to the local data service:

    serviceData = find(sfroot,"-isa","Stateflow.Data",Name="service");

  4. Enable logging for the Dining_area state and the service data:

    diningState.LoggingInfo.DataLogging = true;
    serviceData.LoggingInfo.DataLogging = true;

  5. 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";

Access Signal Logging Data

During simulation, Stateflow saves logged data in a Simulink.SimulationData.Dataset (Simulink) 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 MATLAB workspace.

View Logged Data Through the Simulation Data Inspector

To open the Simulation Data Inspector, in the Simulation tab, select Data Inspector . When you simulate the model, the icon is highlighted to indicate that the Simulation Data Inspector has new simulation data. For more information, see View State Activity by Using the Simulation Data Inspector.

View Logged Data Through the Logic Analyzer

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.

Note

To use the Logic Analyzer, you must have DSP System Toolbox™, SoC Blockset™, or HDL Verifier™.

View Logged Data in the MATLAB Workspace

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

  2. To access logged elements, use the get function. 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]
    

  3. 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
          __________    ____
      
                   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 plot function.

      X = serviceLog.Values.Time;
      Y = serviceLog.Values.Data;
      plot(X,Y,"-o")
      xlabel("Time")
      ylabel("Data")

      Figure that shows the data logged during simulation.

    • 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);

Note

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 values of 0 correspond to when the chart initializes the local data service to 0 at time 0 and when a default transition sets service to 0 at time 1.7076e+06.

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

A[1][1] = 1;
A[1][2] = 1;
produces two different changes in the logged data. In contrast, updating a matrix A in a single command
A = 1;
produces a single change in the logged data, even though the command implies A[i][j] = 1 for all values of i and j.

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.

See Also

Apps

Objects

Functions

Tools

Related Topics