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 Atomic Subcharts
The model sf_atomic_sensor_pair
simulates a redundant sensor
pair by using the 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 by Using the Stateflow User Interface
Open the example library
sf_atomic_sensor_lib
.openExample("sf_atomic_sensor_lib")
In the Library tab, click Locked Library to unlock the library.
Open the chart.
In the Library tab, under Prepare, click Property Inspector.
Enable and customize logging for each state in the chart:
Select the state.
In the Property Inspector, select Log self activity.
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
, andLogFailOnce
.
Open the model
sf_atomic_sensor_pair
. This model contains two instances of the library chart.openExample("sf_atomic_sensor_pair")
Open the Configuration Parameters dialog box.
In the Data Import/Export pane, click Configure Signals to Log to open the Simulink Signal Logging Selector.
In the Simulink Signal Login Selector window, in the Model Hierarchy pane, expand
RedundantSensors
, and clickSensor1
.The library instance inherits the logging properties from the library chart.
Override the logging properties for
Sensor1
: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.Enable logging for the
Fail
andFailOnce
states by selecting the check box in the DataLogging column.Double-click the values in the LoggingName column for the signals
Fail
andFailOnce
, and rename themLogFailSensor1
andLogFailOnceSensor1
, respectively.
Override Logging Properties with the Command-Line API
Open the example library
sf_atomic_sensor_lib
.openExample("sf_atomic_sensor_lib")
Unlock the library.
library = find(sfroot,"-isa","Stateflow.Machine", ... Name="sf_atomic_sensor_lib"); library.Locked = false;
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
Open the model
sf_atomic_sensor_pair
. This model contains two instances of the library chart.open_system("sf_atomic_sensor_pair")
Create a
ModelLoggingInfo
object for the model. This object contains an array namedSignals
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
, andOK
states in atomic subchartSensor1
The library instances of
Fail
,FailOnce
, andOK
states in atomic subchartSensor2
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, seeSimulink.SimulationData.BlockPath
(Simulink).For example, to create block paths for the signals
Fail
,FailOnce
, andOK
in the atomic subchartSensor1
in theRedundantSensors
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");
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);
Override the logging properties for the signals in
Sensor1
:Disable logging for signal
OK
.logInfo.Signals(OKidx).LoggingInfo.DataLogging = false;
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";
Apply the changes.
set_param(bdroot,DataLoggingOverride=logInfo);
See Also
Simulink.SimulationData.ModelLoggingInfo
(Simulink) | Simulink.SimulationData.BlockPath
(Simulink)