Convert Data to Dataset Format
Why Convert to Dataset Format?
You can use the Simulink.SimulationData.Dataset
constructor
to convert a MATLAB® workspace variable that contains data that was logged in one of these formats
to Dataset
format:
Array
Structure
Structure with time
MATLAB
timeseries
objectModelDataLogs
Converting data from other logging formats to Dataset
format
simplifies writing scripts to post-process data logged. For example, a model with multiple
To Workspace blocks can use different data formats. Converting the logged
data to Dataset
format avoids the need to write special code to handle
different formats.
Different simulation modes have different levels of support for data logging formats. Switching between normal and accelerator modes can require changes to the logging formats used.
The conversion to Dataset
format also makes it easier to take
advantage of features that require Dataset
format. You can easily convert
data logged in earlier releases that used a format other than Dataset
to
work well with Dataset
data in a more recent release.
The Dataset
format:
Uses MATLAB
timeseries
objects to store logged data, which allows you to work with logging data in MATLAB without a Simulink® license. For example, to manipulate the logged data, you can use MATLABtimeseries
object functions such asfilter
,detrend
, andresample
.Supports logging multiple data values for a given time step, which is important for Iterator subsystem and Stateflow® signal logging.
By default, the resulting Dataset
object uses the variable name as
the object name. You can use a name-value argument to specify a Dataset
name.
You can also use the concat
function to combine
Dataset
objects into one concatenated Dataset
object.
Results of Conversion
Dataset
objects hold data as elements. To display the elements of a
Dataset
object variable, enter the variable name at the MATLAB command prompt. Each element is an object. The type of the object depends on
the data each element contains. For example, signal logging stores data as
elements, and
state logging in Simulink.SimulationData.Signal
Dataset
format stores data as
elements. Each
element holds data as a MATLAB Simulink.SimulationData.State
timeseries
object. At conversion, the
elements and timeseries
field populate as much as possible from the
converted object.
Format | Conversion Result Notes |
---|---|
MATLAB
| If you log nonbus data, during conversion, the software first adds the
data as a If you log bus data in
|
Structure and structure with time | Structure and structure with time formats do not always contain as much
information as if you log in Conversion populates a
When scope data is
logged in structure format, the logged structure has a
|
Array | Arrays contain little information. For example, there is no block path information. Conversion adds the array to a
|
| Converts data from Note The |
Convert timeseries
object to Dataset
object
Convert data from timeseries
format to Dataset
format. The model vdpConvert
is the vdp
model with two To Workspace blocks that log data to variables named simout
and simout1
.
Simulate the model. By default, To Workspace blocks log data as timeseries
objects.
out = sim("vdpConvert")
out = Simulink.SimulationOutput: simout: [1x1 timeseries] simout1: [1x1 timeseries] SimulationMetadata: [1x1 Simulink.SimulationMetadata] ErrorMessage: [0x0 char]
Use the Simulink.SimulationData.Dataset
constructor to convert the timeseries
objects to Dataset
format.
ds = Simulink.SimulationData.Dataset(out.simout)
ds = Simulink.SimulationData.Dataset '' with 1 element Name BlockPath ____ _________ 1 [1x1 Signal] x1 '' - Use braces { } to access, modify, or add elements using index.
ds1 = Simulink.SimulationData.Dataset(out.simout1)
ds1 = Simulink.SimulationData.Dataset '' with 1 element Name BlockPath ____ _________ 1 [1x1 Signal] x2 '' - Use braces { } to access, modify, or add elements using index.
You can use the concat
function to combine the two Dataset
objects into one concatenated Dataset
object.
dsFinal = concat(ds,ds1)
dsFinal = Simulink.SimulationData.Dataset '' with 2 elements Name BlockPath ____ _________ 1 [1x1 Signal] x1 '' 2 [1x1 Signal] x2 '' - Use braces { } to access, modify, or add elements using index.
Convert Structure to Dataset
Object
Convert a structure to Dataset
format. The model vdpConvertStructure
is the vdp
model with two To Workspace blocks that log data to variables named simout
and simout1
in structure format.
Simulate the model.
out = sim("vdpConvertStructure");
The To Workspace block that saves data to the variable simout
logs data as a structure with time. The To Workspace block that saves data to the variable simout1
logs data as a structure without time.
simout = out.simout
simout = struct with fields:
time: [64x1 double]
signals: [1x1 struct]
blockName: 'vdpConvertStructure/To Workspace'
simout1 = out.simout1
simout1 = struct with fields:
time: []
signals: [1x1 struct]
blockName: 'vdpConvertStructure/To Workspace1'
Convert the structure data from both To Workspace blocks to Dataset
format.
ds = Simulink.SimulationData.Dataset(simout); ds1 = Simulink.SimulationData.Dataset(simout1);
Access the time values for the element ds
.
get(ds,1).Values.time
ans = 64×1
0
0.0001
0.0006
0.0031
0.0157
0.0785
0.2844
0.5407
0.8788
1.2788
⋮
Notice that the time field for simout1
is an empty array. When a structure without time or an array is converted to a Dataset
object, the conversion inserts a time vector that starts at 0 and increments by 1.
Get the time values of the element in ds1
. Because a time vector is not included in the structure simout1
, the time steps for the Dataset
object ds1
do not match the simulation time steps.
get(ds1,1).Values.Time
ans = 64×1
0
1
2
3
4
5
6
7
8
9
⋮
Migrate Legacy Code That Uses ModelDataLogs
For scripts that simulate a model created in a release earlier than R2016a that uses
ModelDataLogs
format for logging, update the code to log in
Dataset
format.
If you have already logged signal data in the ModelDataLogs
format,
you can use the convertToDataset
function to update the
ModelDataLogs
signal logging data to use Dataset
format. For example, update the older_model_data
from
ModelDataLogs
format to Dataset
format.
new_dataset = convertToDataset(logsout,"older_model_data")
Converting a model from using ModelDataLogs
format to using
Dataset
format can require that you modify your existing models and
code in callbacks, functions, scripts, or tests. This table identifies possible issues to
address after converting to Dataset
format.
Possible Issue After Conversion to Dataset Format | Solution |
---|---|
Code in existing callbacks, functions, scripts, or tests that used the
| Check for code that uses For example, suppose that existing code includes this line. logsout.('Subsystem Name').X.data Replace
that code with a get(logsout,"x").Values.data |
Mux block signal names are lost. | The |
Signal Viewer cannot be used for signal logging. | The software does not log signal logging data in the Signal Viewer. Use the signal logging output variable to view the logged data. |
The | The For example, suppose the data in
|
The | If necessary, add signal names. In
In For both |
Test points in referenced models are not logged. | Consider enabling signal logging for test points in a referenced model. |
Script uses | Consider using |
Dataset Conversion Limitations
Converting logged data to
Dataset
format results in aDataset
object that contains all the information that the original logged data included. However, if no corresponding information is available for the otherDataset
properties, the conversion uses default values for that information.When you convert data for a variable-size signal logged using the To Workspace block, the information in the
valueDimensions
field of the structure is lost in the conversion.When you log a bus in array, structure, or structure with time formats, the logged data is organized with:
The first column containing the data for the first signal in the bus
The second column containing data for the second bus signal, and so on
When you convert that data to
Dataset
format, theDataset
object preserves that organization. However, if you log the bus inDataset
format without conversion, the conversion captures the bus data as a structure oftimeseries
objects.If the logged data does not include a time vector, when you convert that data to
Dataset
format, the conversion inserts a time vector. The time vector has one time step for each data value. However, the simulation time steps and theDataset
time steps can vary.Dataset
format ignores the specification of frame signals. Conversion of structure or structure with time data toDataset
format reshapes the data for logged frame signals.
See Also
Objects
Simulink.SimulationData.BlockPath
|Simulink.SimulationData.Signal
|Simulink.SimulationData.DataStoreMemory
|Simulink.SimulationData.Dataset
Functions
addElement
|concat
|find
|get
|getElementNames
|numElements
|setElement