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, the Logic Analyzer, 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 Model Configuration Parameters dialog box.

  2. Select Data Import/Export.

  3. In the Signals pane, select the Signal logging check box to enable logging for the chart. To disable logging, clear the check box.

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

  5. (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).

  6. Click OK.

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, in the sf_semantics_hotel_checkin model:

  1. Open the Hotel chart.

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

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

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

  5. 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, in the sf_semantics_hotel_checkin model:

  1. Open the Hotel chart.

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

  3. 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, in the sf_semantics_hotel_checkin model:

  1. Open the Hotel chart.

  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 and order room service multiple times by toggling the second switch. 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

When you simulate the model, the Simulation Data Inspector icon is highlighted to indicate that it has new simulation data.

  1. To open the Simulation Data Inspector, in the Simulation tab, click the icon Simulation Data Inspector icon.

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

  1. To open the Logic Analyzer, in the Simulation tab, click the icon Logic Analyzer icon.

  2. View, measure and compare the states logged during simulation. See Logic Analyzer (DSP System Toolbox).

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 = 
      Package: Stateflow.SimulationData
             Name: 'Dining Room'
        BlockPath: [1×1 Simulink.SimulationData.BlockPath]
           Values: [1×1 timeseries]
    serviceLog = get(logsout,'service')
    serviceLog = 
      Package: Stateflow.SimulationData
             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
          __________    ____
          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;

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

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




Related Topics