Main Content

Log Data in Library Charts

In Simulink®, you can create your own block libraries as a way to reuse the functionality of blocks or subsystems in one or more models. Similarly, you can reuse a set of Stateflow® algorithms by encapsulating the functionality in a library chart.

As with other Simulink block libraries, you can specialize each instance of chart library blocks in your model to use different data types, sample times, and other properties. Library instances that inherit the same properties can reuse generated code.

For more information about Simulink block libraries, see Custom Libraries (Simulink).

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.

Override 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 Override Logging Properties with the Logging Selector
Command-line interfaceSee Override Logging Properties with the Command-Line API

Override Logging Properties in Atomic Subcharts

The model sf_atomic_sensor_pair 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.

Override Logging Properties with the Logging Selector

  1. Open the example library sf_atomic_sensor_lib.

    openExample("sf_atomic_sensor_lib")
  2. Unlock the library. In the Simulation tab, click Locked Library.

  3. In the Simulink Editor, select the Stateflow SingleSensor chart. In the Simulation tab, click Log States from List.

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

  5. Open the model sf_atomic_sensor_pair. This model contains two instances of the library chart.

    openExample("sf_atomic_sensor_pair")
  6. Open the Configuration Parameters dialog box.

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

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

    Each instance inherits logging properties from the library chart.

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

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

    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. Log the signals Fail, FailOnce, and OK in the SingleSensor chart using these commands:

    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 a vector 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 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

  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. For example, 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.

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

    1. Disable logging for signal OK:

      logInfo.Signals(OKidx).LoggingInfo.DataLogging = false;
    2. Append the text Sensor1 to the logging names for Fail and FailOnce:

      % Enable custom naming
      logInfo.Signals(failidx).LoggingInfo.NameMode = true;
      logInfo.Signals(failOnceidx).LoggingInfo.NameMode = true;
      
      % Enter the custom name
      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)