Skip to Main Content Skip to Search
Product Documentation

Logging Data Values and State Activity

What You Can Log During Chart Simulation

When you simulate a chart, you can log values for local data and state activity into Simulink objects. After simulation, you can access these objects in the MATLAB workspace and use them to report and analyze the values.

When you log a state, its value is 1 when active and 0 when inactive.

Logging Stateflow data and state activity follows the same general guidelines as for logging signals in Simulink models.

See Also

Workflow for Logging States and Local Data

The workflow for logging chart local data and state activity is similar to the workflow for logging signals in a model:

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

    See Enabling Signal Logging and Choosing a Logging Format.

  2. Configure states and local data for signal logging, which includes controlling how much output the simulation generates.

    See Configuring States and Local Data for Logging.

  3. Simulate the chart.

  4. Access the logged data.

    See Accessing Logged Data.

Example for Logging Workflow

The procedures that take you through the logging workflow use sf_semantics_hotel_checkin. The model simulates a hotel check-in process. To open the model, click sf_semantics_hotel_checkin or type sf_semantics_hotel_checkin at the MATLAB command prompt.

In this model, the chart Hotel controls activities that trigger transitions to different rooms in the hotel. The chart contains a hierarchy of nested states and uses two local variables:

Local VariableDescription
move_bagsIndicates whether bags should move to another room or stay in the current room.
serviceAccumulates the number of room service calls.

Enabling Signal Logging and Choosing a Logging Format

  1. Open the chart and select Simulation > Configuration Parameters.

  2. Select Data Import/Export.

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

    Signal logging is enabled by default for models and charts. To disable logging, clear the check box.

  4. Optionally, 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 (see Signal Logging Object).

  5. Specify a format for logged data from the Signal logging format menu.

    See Supported Formats for Logged Data.

  6. Click OK.

Supported Formats for Logged Data

Stateflow charts support the same formats for logged data as Simulink models:

FormatDescription
DatasetStores logged data as MATLAB timeseries objects in objects of the value class Simulink.SimulationData.Dataset.
ModelDataLogsStores logged data in objects of the handle class Simulink.ModelDataLogs.

Advantages of Dataset Format.  The Dataset format offers several advantages over the ModelDataLogs format for logging chart data and state activity:

See Also.  

Configuring States and Local Data for Logging

Properties to Configure for Logging

You can configure the same properties for logging states and local data in a chart as you can for logging signals in a model:

PropertyDescription
Log signal dataSaves the signal's value to the MATLAB workspace during simulation.
Logging nameName of the logged signal. Defaults to the original name of the state or local data. To rename the logged signal, select Custom and enter a new name. For guidance on when to use a different name for a logged signal, see Specifying a Logging Name in the Simulink documentation.
Limit data points to lastLimits the amount of data logged to the most recent samples. See Limit Data Points to Last in the Simulink documentation.
Decimation valueLimits the amount of data logged by skipping samples. For example, a decimation factor of 2 saves every other sample. See Decimation in the Simulink documentation.

See Also.  

Choosing a Configuration Method for Logging

There are several ways to configure states and local data for logging:

MethodWhen to Use
Logging Individual States and DataConfigure states or local data for logging one at a time from inside the chart.
Logging Multiple Signals At OnceConfigure multiple signals for logging from a list of all states and local data.
Logging Chart Signals Using the Command-Line APIConfigure logging properties programmatically.

Logging Individual States and Data

  1. Open the properties dialog box for the state or local data.

    For:Do This:
    StatesRight-click the state and select Properties.
    Local dataRight-click the state or transition that uses the local data and select Explore > (data)variable_name.

  2. In the properties dialog box, click the Logging tab.

  3. Modify properties as needed, as described in Properties to Configure for Logging.

For example, from the Hotel chart of the sf_semantics_hotel_checkin model:

  1. Open the properties dialog box for the service local data and select the Log signal data check box.

  2. Open the properties dialog box for the Dining_area state, select the Log signal data check box, and change the logging name to Dining_Room.

See Also.  

Logging Multiple Signals At Once

  1. In the model, right-click the Stateflow chart and select Log Chart Signals.

    The Stateflow Signal Logging dialog box opens, showing all states and local data. These chart objects are the signals you can log.

  2. Select the check box next to each signal you want to log.

    The Log signal data check box is selected automatically for each signal you log. For example, in the Hotel chart of the sf_semantics_hotel_checkin model, log the Check_in.Checked_in.Executive_suite.Dining_area state and local variable service:

  3. For each signal you select, modify the properties of what gets captured in the log.

    For example, change the logging name of Check_in.Checked_in.Executive_suite.Dining_area to Dining_Room.

    For a description of each property, see Properties to Configure for Logging.

Logging Chart Signals Using the Command-Line API

  1. Open the model that contains the chart.

    For example, open the sf_semantics_hotel_checkin model, which has a chart called Hotel.

  2. Get the states whose activity you want to log.

    For example, get the Dining_area state in the Hotel chart:

    rt = sfroot;
    da_state = rt.find('-isa','Stateflow.State','Name','Dining_area');

    Get the local data you want to log.

    For example, get the service local data in the Hotel chart:

    svc_data = rt.find('-isa','Stateflow.Data','Name','service');
  3. Enable logging for states and data.

    For example, enable logging for the Dining_area state and the service data:

    da_state.LoggingInfo.DataLogging = 1;
    svc_data.LoggingInfo.DataLogging = 1;
  4. Modify logging properties as needed.

    For example, change the logged name of the Dining_area state. By default, the logged name is the hierarchical signal name, which is Check_in.Checked_in.Executive_suite.Dining_area. To assign the shorter, custom name of Dining_Room:

    % Enable custom naming
    da_state.LoggingInfo.NameMode = 'Custom';
    
    % Enter the custom name
    da_state.LoggingInfo.UserSpecifiedLogName = 'Dining_Room';

See Also.  

Logging Multiple Signals At Once

Accessing Logged Data

Signal Logging Object

During simulation, Stateflow saves logged data in a signal logging object, which you can access in the MATLAB workspace. The type of signal logging object depends on the signal logging format that you choose.

FormatSignal Logging Object
DatasetSimulink.SimulationData.Dataset
ModelDataLogsSimulink.ModelDataLogs

The default name of the signal logging object is logsout.

See Also.  

Accessing Logged Data Saved in Dataset Format

  1. View the signal logging object in the MATLAB environment.

    For example:

    1. Start simulating the sf_semantics_hotel_checkin model using the Dataset signal logging format.

    2. When the Front_desk state becomes active, check in to the hotel by toggling the first switch.

    3. When the Bedroom state in the Executive_suite state becomes active, order room service multiple times, for example, by toggling the second switch 10 times.

    4. Stop simulation.

    5. Enter:

      logsout

      Result:

        Simulink.SimulationData.Dataset
        Package: Simulink.SimulationData
      
        Characteristics:
                    Name: 'logsout'
          Total Elements: 2
      
        Elements:
          1: 'Dining_Room'
          2: 'service'

      The output indicates:

      • logsout is a Simulink object of type SimulationData.Dataset.

      • Two elements were logged.

  2. Use the getElement method to access logged elements by index and by name.

    For example:

    • To access logged activity for the Check_in.Checked_in.Executive_suite.Dining_area state:

      By:Enter:
      Indexlogsout.getElement(1)
      Namelogsout.getElement('Dining_Room')
      Block path
      1. logsout.getElement(1).BlockPath

        Returns:

        • Block Path: 'sf_semantics_hotel_checkin/Hotel'

        • SubPath: 'Check_in.Checked_in.Executive_suite.Dining_area'

      2. bp = Simulink.BlockPath('sf_semantics_hotel_checkin/Hotel');

      3. bp.SubPath = 'Check_in.Checked_in.Executive_suite.Dining_area';

      4. logsout.getElement(bp)

      The result is a Stateflow.SimulationData.State object:

        Stateflow.SimulationData.State
        Package: Stateflow.SimulationData
      
        Properties:
               Name: 'Dining_Room'
          BlockPath: [1x1 Simulink.SimulationData.BlockPath]
             Values: [1x1 timeseries]
      
    • To access logged activity for the service local data:

      By:Enter:
      Indexlogsout.getElement(2)
      Namelogsout.getElement('service')
      Block path
      1. logsout.getElement(2).BlockPath

        Returns:

        • Block Path: 'sf_semantics_hotel_checkin/Hotel'

        • SubPath: 'service'

      2. bp = Simulink.BlockPath('sf_semantics_hotel_checkin/Hotel');

      3. bp.SubPath = 'service';

      4. logsout.getElement(bp)

      The result is a Stateflow.SimulationData.Data object:

         Stateflow.SimulationData.Data
        Package: Stateflow.SimulationData
      
        Properties:
               Name: 'service'
          BlockPath: [1x1 Simulink.SimulationData.BlockPath]
             Values: [1x1 timeseries]

    The logged values for Stateflow.SimulationData.State and Stateflow.SimulationData.Data objects are stored in the Values property as Simulink objects of type Timeseries.

  3. Access logged data and time through the Values property.

    For example:

    For:Enter:
    Datalogsout.getElement(1).Values.Data;
    Timelogsout.getElement(1).Values.Time;

  4. View the logged data.

    See Viewing Logged Data.

Accessing Logged Data Saved in ModelDataLogs Format

  1. View the signal logging object in the MATLAB environment.

    For example:

    1. Start simulating the sf_semantics_hotel_checkin model using the ModelDataLogs signal logging format.

    2. When the Front_desk state becomes active, check in to the hotel by toggling the first switch.

    3. When the Bedroom state in the Executive_suite state becomes active, order room service multiple times, for example, by toggling the second switch 10 times.

    4. Stop simulation.

    5. Enter:

      logsout

      Result:

       Simulink.ModelDataLogs (sf_semantics_hotel_checkin):
        Name                   Elements  Simulink Class
      
        Hotel                     2      StateflowDataLogs

      The output indicates:

      • logsout is a Simulink object of type Simulink.ModelDataLogs.

      • Two elements were logged, all in the Hotel chart.

  2. List the logged states and local data.

    For example:

    logsout.Hotel
    

    Result:

    Simulink.StateflowDataLogs (Hotel):
      Name                   Elements  Simulink Class
    
      service                   1      Timeseries
      Dining_Room               1      Timeseries

    The logged signals are stored as Simulink objects of type Timeseries.

  3. Use dot notation to access logged values for data or state activity.

    Because of the way Simulink stores logged signals in ModelDataLogs format, you must use dot notation to access logged data for Stateflow objects below the chart level in the model hierarchy.

    For example, to access logged values for service data, enter:

    logsout.Hotel.service
    

    Result:

              Name: 'service'
         BlockPath: 'sf_semantics_hotel_checkin/Hotel/service'
         PortIndex: 1
        SignalName: 'service'
        ParentName: 'service'
          TimeInfo: [1x1 Simulink.TimeInfo]
              Time: [560498x1 double]
              Data: [560498x1 double]

    A structure stores all logging information. The logged data values reside in the Data field and the logged times reside in the Time field. The size of your Time and Data vectors might differ from those shown here, depending on how you interact with the switches in the model.

  4. View the logged data.

    See Viewing Logged Data.

Viewing Logged Data

Use one of the following approaches to view logged data.

ApproachAction
View logged data in a figure window

Use the plot function for the Timeseries object.
For example, in the Hotel chart, plot logged values over time in ModelDataLogs format for the local variable service by entering:

logsout.Hotel.service.plot
View logged data in a spreadsheet

Pass a numeric, cell, or logical array of logged values to the xlswrite function.
For example, in the Hotel chart, to view logged activity over time in Dataset format for the Check_in.Checked_in.Executive_suite.Dining_area state:

  1. Assign logged Dining_Room time and data values to an array A:

    A = [logsout.getElement('Dining_Room').Values.Time ...
        logsout.getElement('Dining_Room').Values.Data];
  2. Export the data to an Excel® file named dining_log.xls:

    xlswrite('dining_log.xls',A);
  3. Open dining_log.xls in Excel.

Logging Data in Library Charts

How Library Log Settings Influence Linked Instances

Chart instances inherit logging properties from the library chart to which they are linked. You can override logging properties in the instance, but only for signals you select in the library. You cannot select additional signals to log from the instance.

Overriding Logging Properties in Chart Instances

To override properties of logged signals in chart instances, use one of the following approaches.

ApproachHow To Use
Simulink Signal Logging Selector dialog boxSee Overriding Logging Properties with the Logging Selector
Command-line interfaceSee Overriding Logging Properties with the Command-Line API

Example: Override Logging Properties in Atomic Subcharts

This example uses sf_atomic_sensor_pair. This model simulates a redundant sensor pair as atomic subcharts Sensor1 and Sensor2 in the chart RedundantSensors. Each atomic subchart contains instances of the states Fail, FailOnce, and OK from the library chart sf_atomic_sensor_lib.

Overriding Logging Properties with the Logging Selector.  

  1. Open the example library by clicking sf_atomic_sensor_lib or typing sf_atomic_sensor_lib at the MATLAB command prompt.

  2. In the library, right click SingleSensor and select Log Chart Signals.

  3. In Stateflow Signal Logging dialog box, set the following logging properties, then click OK.

    For Signal:What to Specify:
    Fail
    • Select the Log signal data check box.

    • Change Logging name to the custom name LogFail.

    • Click Apply.

    FailOnce
    • Select the Log signal data check box.

    • Change Logging name to the custom name LogFailOnce.

    • Click Apply.

    OK
    • Select the Log signal data check box.

    • Change Logging name to the custom name LogOK.

    • Click Apply.

  4. Open the model that contains instances of the library chart by clicking sf_atomic_sensor_pair or typing sf_atomic_sensor_pair at the MATLAB command prompt.

  5. Open the RedundantSensors chart and select Simulation > Configuration Parameters.

  6. In the Data Import/Export pane, click Configure Signals to Log to open the Simulink Signal Logging Selector.

  7. In the Model Hierarchy pane, expand RedundantSensors, and click Sensor1 and Sensor2.

    Each instance inherits logging properties from the library chart. For example:

  8. Now, override some logging properties for Sensor1:

    1. In the Model Hierarchy pane, select Sensor1.

    2. Change Logging Mode to Override signals.

      The selector clears all DataLogging check boxes for the model.

    3. Enable logging only for the Fail and FailOnce states in Sensor1:

      Select DataLogging for these two signals. Leave DataLogging cleared for the OK signal.

    4. Append the string Sensor1 to the logging names for Fail and FailOnce:

      Double-click the logging names for signals Fail and FailOnce, and rename them LogFailSensor1 and LogFailOnceSensor1, respectively.

    The settings should look like this:

Overriding Logging Properties with the Command-Line API.  

  1. Open the example library by clicking sf_atomic_sensor_lib or typing sf_atomic_sensor_lib at the MATLAB command prompt.

  2. Log the signals Fail, FailOnce, and OK in the SingleSensor chart using these commands:

    % Get states in the SingleSensor chart
    rt=sfroot;
    states = rt.find('-isa', 'Stateflow.State');
    
    % Enable logging for each state
    for i = 1: length(states)
       states(i).LoggingInfo.DataLogging = 1;
    end
    

  3. Open the model that contains instances of the library chart by clicking sf_atomic_sensor_pair or typing sf_atomic_sensor_pair at the MATLAB command prompt.

  4. Create a ModelLoggingInfo object for the model.

    This object contains a vector Signals that stores all logged signals.

    mi = Simulink.SimulationData.ModelLoggingInfo. ...
    createFromModel('sf_atomic_sensor_pair')
    

    The result is:

    mi = 
    
      Simulink.SimulationData.ModelLoggingInfo
      Package: Simulink.SimulationData
    
      Properties:
                         Model: 'sf_atomic_sensor_pair'
                   LoggingMode: 'OverrideSignals'
        LogAsSpecifiedByModels: {}
                       Signals: [1x6 Simulink.SimulationData.SignalLoggingInfo]

    The Signals vector contains the signals marked for logging in the library chart:

    • Library instances of Fail, FailOnce, and OK states in atomic subchart Sensor1

    • Library instances of Fail, FailOnce, and OK states in atomic subchart Sensor2

  5. Make sure that LoggingMode equals 'OverrideSignals'.

  6. Create a block path to each logged signal whose properties you want to override.

    To access signals inside Stateflow charts, use Simulink.SimulationData.BlockPath(paths, subpath), where subpath represents a signal inside the chart.

    To create block paths for the signals Fail, FailOnce, and OK in the atomic subchart Sensor1 in the RedundantSensors chart:

    failPath = Simulink.SimulationData. ...
    BlockPath('sf_atomic_sensor_pair/RedundantSensors/Sensor1','Fail')
    
    failOncePath = Simulink.SimulationData. ...
    BlockPath('sf_atomic_sensor_pair/RedundantSensors/Sensor1','FailOnce')
    
    OKPath = Simulink.SimulationData. ...
    BlockPath('sf_atomic_sensor_pair/RedundantSensors/Sensor1','OK')

  7. Get the index of each logged signal in the Simulink.SimulationData.BlockPath object.

    To get the index for the signals Fail, FailOnce, and OK:

    failidx = mi.findSignal(failPath);
    failOnceidx = mi.findSignal(failOncePath);
    OKidx = mi.findSignal(OKPath);

  8. Override some logging properties for the signals in Sensor1:

    1. Disable logging for signal OK:

      mi.Signals(OKidx).LoggingInfo.DataLogging = 0;
    2. Append the string Sensor1 to the logging names for Fail and FailOnce:

      % Enable custom naming
      mi.Signals(failidx).LoggingInfo.NameMode = 1;
      mi.Signals(failOnceidx).LoggingInfo.NameMode = 1;
      
      % Enter the custom name
      mi.Signals(failidx).LoggingInfo.LoggingName = 'LogFailSensor1';
      mi.Signals(failOnceidx).LoggingInfo.LoggingName = 'LogFailOnceSensor1';

  9. Apply the changes:

    set_param(bdroot, 'DataLoggingOverride', mi);
See Also.  

How Stateflow Logs Multidimensional Data

Stateflow logs each update to a multidimensional signal as a single change. For example, an update to a 2-by-2 matrix A during simulation is logged as a single change, not as four changes (one for each element):

UpdateIs Logged As
A = 1;A single change, even though the statement implies all A[i] = 1
A[1][1] = 1;
A[1][2] = 1;
Two different changes

Limitations on Logging Data

You cannot log bus data.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS