This example shows how to convert MATLAB® time-series data to Dataset
format.
myvdp_timeseries
is the vdp
model with two To
Workspace blocks configured for simout and
simout1 logging data in MATLAB timeseries format. Consider using a procedure like this one if you have models
that use To Workspace blocks to log data to MATLAB timeseries format.
Use the Simulink.SimulationData.Dataset
constructor to
convert the MATLAB timeseries data to Dataset
format and then concatenate the
two data sets.
Starting with the vdp
model, add two To Workspace
blocks to the model as shown.
Set the Save format parameter of both blocks. Set
Timeseries
.
Save the model as myvdp_timeseries
.
Simulate the model.
The simulation logs data using the To Workspace blocks.
Access the signal logging format, logsout
.
logsout
logsout = Simulink.SimulationData.Dataset Package: Simulink.SimulationData Characteristics: Name: 'logsout' Total Elements: 2 Elements: 1: 'x1' 2: 'x2' -Use get or getElement to access elements by index or name. -Use addElement or setElement to add or modify elements. Methods, Superclasses
Convert the MATLAB time-series data from both To Workspace blocks to
Dataset
.
ds = Simulink.SimulationData.Dataset(simout); ds1 = Simulink.SimulationData.Dataset(simout1);
ds is the variable name of the first To Workspace block data. ds1 is the variable name of the second To Workspace block data.
Concatenate both datasets into dsfinal. Observe that the format of
dsfinal matches that of logsout
.
dsfinal = ds.concat(ds1)
dsfinal = Simulink.SimulationData.Dataset Package: Simulink.SimulationData Characteristics: Name: 'simout' Total Elements: 2 Elements: 1: 'x1' 2: 'x2' -Use get or getElement to access elements by index or name. -Use addElement or setElement to add or modify elements. Methods, Superclasses
This example shows how to convert structure without time data to
Dataset
format. myvdp_structure
is the
vdp
model with two To
Workspace blocks configured for simout and simout1 logging data in structure format,
as shown.
If you have models that use To Workspace blocks to log data to structure
format, consider using a procedure like this one to convert them to Dataset
format.
Starting with the vdp
model, add two To Workspace
blocks to the model as shown.
In the Save format parameter of both blocks, select
Structure
.
Enable signal logging for the two signals going to the two To Workspace
blocks to log in Ds
format.
Save the model as myvdp_structure
.
Simulate the model.
The simulation logs data using the To Workspace blocks.
Convert the structure data from both To Workspace blocks to
Dataset
.
ds = Simulink.SimulationData.Dataset(simout); ds1 = Simulink.SimulationData.Dataset(simout1);
simout is the variable name of the first To Workspace block data. simout1 is the variable name of the second To Workspace block data.
With the conversion of structure without time or an array, time starts at t=0 and increments by 1.
Get the values of the first element in ds.
ds.get(1).Values.Time
ans = 0 1 2 3 . . . 61 62 63
Get the time values of the first element from signal logging.
logsout.get(1).Values.Time
ans = 0 0.0001 0.0006 0.0031 . . . 19.2802 19.6802 20.0000
Observe the discrepancy in timestamps between
Data logged in structure without time that you convert to
Dataset
format
Data logged in Dataset
format
When you use the default Dataset
signal logging format,
Simulink® saves the logging data in a Simulink.SimulationData.Dataset
object. For information about extracting signal data from that object, see the
reference page.Simulink.SimulationData.Dataset
The Simulink.SimulationData.Dataset
object contains a
object for each logged signal.Simulink.SimulationData.Signal
For bus signals, the Simulink.SimulationData.Signal
object contains a
structure of MATLAB
objects.timeseries
The Simulink.SimulationData.Dataset
class provides two methods for
accessing the signal logging data and its associated information.
Name | Description |
---|---|
You
can also use the | Get element or collection of elements from the dataset, based on index, name, or block path. |
Get number of elements in the dataset. |
For example of accessing signal logging data that uses the
Dataset
format, see
.Simulink.SimulationData.Dataset
Signal logging data for an array of buses uses Dataset
signal
logging format.
The general approach to access data for a specific signal in an array of buses is:
Use a Simulink.SimulationData.Dataset.get
(or
getElement
) method to access a specific signal in the logged data
(by default, the logsout
variable).
To get the values, index within the array of buses.
Index again to get data for a specific bus.
For example, to obtain the signal logging data for the Constant6 block in the
ex_log_nested_aob
model, for the topBus
signal that
feeds the Terminator block:
logsout.getElement('topBus').Values.a(2,2).firstConst.data
Here are additional examples of accessing array of buses signal logging data. For
another example that shows how to log array of buses data, see sldemo_mdlref_bus
.
For a model with multiple signals that have the same signal name, signal logging data
includes a Simulink.SimulationData.Signal
object for each signal that has
a duplicate name.
To access a specific signal that has a duplicate name, use one of these approaches:
To find the data for the specific signal, visually inspect the displayed output of
Simulink.SimulationData.Signal
objects.
Use the Simulink.SimulationData.Dataset.getElement
method,
specifying the block path for the source block of the signal.
To iterate through the signals with a duplicate signal name, create a script using
the Simulink.SimulationData.Dataset.getElement
method with an index
argument.
Use the Signal Properties dialog box to specify a different name. Consider using this approach when the signals with a duplicate name do not appear in multiple instances of a referenced model in normal mode.
In the model, right-click the signal.
In the context menu, select Properties.
In the Signal Properties dialog box, set Logging name to
Custom
and specify a different name than the signal
name.
Simulate the model and use the
Simulink.SimulationData.Dataset.getElement
method with a name
argument.
Tip
Alternatively, you can use the Signal Logging Selector to access a specific signal. For details, see Override Signal Logging Settings with Signal Logging Selector.
To handle newline characters in logging names in signal logging data that uses
Dataset
format, use a sprintf
command within
a getElement
call. For example:
topOut.getElement(sprintf('INCREMENT\nBUS'))
Simulink.SimulationData.Dataset