The approach you use to log data for a signal in a For Each subsystem depends on whether the signal is a:
Nonbus signal — Log directly in a For Each subsystem
A bus or array of buses signal — Use one of these approaches:
Use a Bus Selector block to select the signals you want to log and mark those signals for signal logging. This approach works well for many models.
Attach the signal to an Outport block and log the signal outside of the For Each subsystem. Use this approach when you want to log a whole bus signal, and that bus signal includes many bus element signals.
Note
You cannot log bus signals directly in a For Each subsystem.
You cannot log a signal inside a referenced model that is inside a For Each subsystem if either of these conditions exists:
The For Each subsystem is in a model simulating in rapid accelerator mode.
The For Each subsystem itself is in a model referenced by a Model block in accelerator mode.
The data for each logged signal in a For Each subsystem is saved in a separate
Dataset
element as a Simulink.SimulationData.Signal
object. The format of the logged signal data depends on how you set the Dataset
signal format configuration parameter:
If the setting is timeseries
, then each signal object contains an
array of MATLAB®
timeseries
objects. The array keeps the data from different For Each
iteration separate.
If the setting is timetable
, then each signal object contains a
cell array of MATLAB
timetable
objects. The dimensions of this array match the number of For
Each iterations. For example, if the For Each subsystem has three iterations, then the
logged data has a 3x1
array of timeseries
or
timetable
objects. For nested For Each subsystems, each layer of
nesting adds another dimension to the logged data.
This example logs a signal in a nested For Each subsystem.
Open the ex_loginsideforeach_nested
model.
open_system(docpath(fullfile(docroot,'toolbox','simulink',... 'examples','ex_loginsideforeach_nested.slx')))
In the Simulink® Editor, open the For Each Subsystem1
block, and inside
that subsystem, open the For Each Subsystem2
block.
Simulate the model and examine the signal logging data for the first iteration of
the top subsystem and the third iteration of the bottom subsystem. The
2x3
timeseries
results from two iterations at the first For Each level
and three iterations at the second (nested) level
sim('ex_loginsideforeach_nested'); logsout.get('nestedDelay')
ans = Simulink.SimulationData.Signal Package: Simulink.SimulationData Properties: struct with fields: Name: 'nestedDelay' PropagatedName: '' BlockPath: [1×1 Simulink.SimulationData.BlockPath] PortType: 'outport' PortIndex: 1 Values: [2×3 timeseries]
Return the values of the nestedDelay
object.
logsout.get('nestedDelay').Values(1,3)
timeseries Common Properties: Name: 'nestedDelay' Time: [5x1 double] TimeInfo: [1x1 tsdata.timemetadata] Data: [1x1x5 double] DataInfo: [1x1 tsdata.datametadata]
This example logs a two bus signal in a For Each subsystem. For one bus signal, you use a Bus Selector block and then log each selected signal. For the other bus signal, you use Outport blocks and log outside of the For Each subsystem.
Open the ex_for_each_log_bus
model.
open_system(docpath(fullfile(docroot,'toolbox','simulink',... 'examples','ex_for_each_log_bus.slx')))
In the Simulink Editor, open the For Each Subsystem
block.
To log the signals in the limits
bus signal, the signal is
branched to a Bus Selector block, and each of the bus element signals is
marked for signal logging.
To log the whole COUNTERBUS
signal, the bus signal is connected
to an Outport block. The output signal from the For Each subsystem is
marked for signal logging. To have the bus signal cross the subsystem boundary, the
Bus Creator block that creates the COUNTERBUS
signal
has the Output data type parameter set to Bus:
COUNTERBUS
and Output as nonvirtual bus check box
selected.
Simulate the model and examine the signal logging output. Focus on one of the bus element signals logged inside the For Each subsystem and on the bus signal logged outside of the For Each subsystem.
sim('ex_for_each_log_bus');
logsout
Simulink.SimulationData.Dataset 'logsout' with 3 elements Name BlockPath ________________________ ________________________________________ 1 [1x1 Signal] OutsideForEach ex_for_each_log_bus/For Each Subsystem 2 [1x1 Signal] <lower_saturation_limit> ...g_bus/For Each Subsystem/Bus Selector 3 [1x1 Signal] <upper_saturation_limit> ...g_bus/For Each Subsystem/Bus Selector - Use braces { } to access, modify, or add elements using index.
Return the values of the lower_saturation_limit
object.
logsout{2}.Values
3×1 timeseries array with properties: Events Name UserData Data DataInfo Time TimeInfo Quality QualityInfo IsTimeFirst TreatNaNasMissing Length
Return the values of the OutsideForEach
object.
logsout{1}.Values
ans = 3×1 struct array with fields: data limits
If the Dataset
signal format is timetable
,
then the output is a cell array of timetable
objects. For
example:
out = sim('ex_for_each_log_bus','DatasetSignalFormat','timetable'); out.logsout{2}.Values
ans = 3x1 cell array {11x1 timetable} {11x1 timetable} {11x1 timetable}