Main Content

Log Data in Library Charts

When analyzing how a system operates, you can log chart data to track performance. Unlike normal Stateflow® charts, you can use library charts to program logging settings that are inherited by all linked instances. Reusing linked library charts makes it easier to track and analyze behavior across different models using the same foundational logic. Library charts provide the flexibility to customize logging for specific instances, capture the necessary data for detailed analysis, and monitor performance.

For more information about Stateflow chart libraries, see Reuse Charts in Models with Chart Libraries.

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.

Override Logging Properties in a Linked Chart

In this example, you override the logging properties of a Chart block linked to a library chart, to customize the data logging for specific signals, so that you capture the correct information for multiple charts.

Open the model sf_atomic_sensor_pair. This model contains two instances of the library chart SingleSensor from the library sf_atomic_sensor_lib.

openExample("sf_atomic_sensor_pair")

The sf_atomic_sensor_pair model.

The model sf_atomic_sensor_pair simulates a redundant sensor pair by using the linked atomic subcharts Sensor1 and Sensor2 in the chart RedundantSensors. Sensor1 and Sensor2 are both copies of the library chart SingleSensor in the from the library sf_atomic_sensor_lib.

RedundantSensors chart

Each atomic subchart contains instances of the states Fail, FailOnce, and OK.

Override Logging Properties by Using the Stateflow User Interface

  1. Open the example library sf_atomic_sensor_lib.

    openExample("sf_atomic_sensor_lib")
  2. In the Library tab, click Locked Library to unlock the library.

  3. Open the SingleSensor chart.

  4. In the Library tab, under Prepare, click Property Inspector.

  5. Enable and customize logging for every state in the chart:

    1. Select the state.

    2. In the Property Inspector, select Log self activity.

    3. In the Property Inspector, in the Logging Name drop-down list, select Custom.

      In the text field, enter Log followed by the state name. For example, LogOK, LogFail, and LogFailOnce.

  6. In the sf_atomic_sensor_pair model, open the Configuration Parameters dialog box.

  7. In the Data Import/Export pane, click Configure Signals to Log.

  8. In the Simulink Signal Login Selector window, in the Model Hierarchy pane, expand RedundantSensors>Sensor1.

    The library instance inherits the logging properties from the library chart.

    Simulink signal logging selector that shows logging properties inherited from the library chart.

  9. Override the logging properties for Sensor1:

    1. At the top of the window, change Logging Mode to Override signals. In the Contents of pane, the selector clears the values of the DataLogging check boxes for both library charts.

    2. Enable logging for the Fail and FailOnce states by selecting the check box in the DataLogging column.

    3. Double-click the values in the LoggingName column for the signals Fail and FailOnce, and rename them LogFailSensor1 and LogFailOnceSensor1, respectively.

    Simulink signal logging selector that shows different logging properties than the library chart.

Override Logging Properties with the Command-Line API

  1. Open the example library sf_atomic_sensor_lib.

    openExample("sf_atomic_sensor_lib")
  2. Unlock the library.

    library = find(sfroot,"-isa","Stateflow.Machine", ...
        Name="sf_atomic_sensor_lib");
    library.Locked = false;
  3. Create an array that contains every state in the library chart. Log the signals for each state.

    states = find(library,"-isa","Stateflow.State");
    
    for i = 1: length(states)
       states(i).LoggingInfo.DataLogging = true;
    end
  4. Open the model sf_atomic_sensor_pair. This model contains two instances of the library chart.

    open_system("sf_atomic_sensor_pair")
  5. Create a ModelLoggingInfo object for the model. This object contains an array named Signals that stores all logged signals.

    logInfo = Simulink.SimulationData.ModelLoggingInfo.createFromModel("sf_atomic_sensor_pair")
    logInfo = 
    
      ModelLoggingInfo with properties:
    
                         Model: 'sf_atomic_sensor_pair'
                   LoggingMode: 'OverrideSignals'
        LogAsSpecifiedByModels: {}
                       Signals: [1×6 Simulink.SimulationData.SignalLoggingInfo]

    The Signals array contains the signals marked for logging in the library chart, including:

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

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

  6. Create a block path to the logged signals whose properties you want to override.

    To access signals inside Stateflow charts, use the Simulink.SimulationData.BlockPath object. For more information, see Simulink.SimulationData.BlockPath (Simulink).

    For example, to create block paths for the signals Fail, FailOnce, and OK in the atomic subchart Sensor1 in the RedundantSensors chart, enter:

    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.

    failidx = logInfo.findSignal(failPath);
    failOnceidx = logInfo.findSignal(failOncePath);
    OKidx = logInfo.findSignal(OKPath);
  8. Override the logging properties for the signals in Sensor1:

    1. Disable logging for signal OK.

      logInfo.Signals(OKidx).LoggingInfo.DataLogging = false;
    2. Enable and set custom names.

      logInfo.Signals(failidx).LoggingInfo.NameMode = true;
      logInfo.Signals(failOnceidx).LoggingInfo.NameMode = true;
      
      logInfo.Signals(failidx).LoggingInfo.LoggingName = "LogFailSensor1";
      logInfo.Signals(failOnceidx).LoggingInfo.LoggingName = "LogFailOnceSensor1";
  9. Apply the changes.

    set_param(bdroot,DataLoggingOverride=logInfo);

See Also

(Simulink) | (Simulink)