Guy on Simulink

Simulink & Model-Based Design

Another Good Reason to Log Simulation Data in Dataset Format

Today I am happy to welcome guest blogger Mariano Lizarraga Fernandez. A few days ago, Mariano came to me looking for help understanding a Simulink behavior that a user was not able to explain. Once we figured it out, we thought it would be good to share with you.


In R2015b we introduced the possibility to save states and outputs in Dataset format. In R2016a, these capabilities were further extended to log units, and for large amounts of data, to be able to log data directly to a MAT-file. I could list tons of reasons why the Dataset format is more convenient than other options, but for today what I want to share is an example where the dataset format could have helped avoiding lots of confusion.

Serializing Data from frames

I was recently helping a customer who was logging frames-based signals with a variable-step solver and saving it to the workspace. If the frame-based data was serialized (i.e each frame was stacked on to the last one creating a n x 1 vector) and plotted, the data showed what looked like every once in a while a frame was repeated, causing discontinuities every given number of frames.

Take for instance the following model, which uses a variable-step discrete solver and contains two different sample times. The model is configured to save outputs in the Structure with Time format:

Frames Example 1

Serializing and plotting the outport's 'Out1' data, one can see the repeated frames:

Frames output

The Structure with Time documentation explains that it contains a single vector of the simulation times. This has two implications:

  1. The time vector contains the union of all sample time vectors used in the model.
  2. For the signal values to be consistent with the time vector, its value must be logged at every sample time contained in the time vector.

Using Dataset to Log Data at Different Sample Rates

To better understand these implications, let's get rid of the frame and consider the following model which saves 3 ramp signals:

Simple Model

Let us now run the simulation and log the output of all three outports in two different formats: Structure With Time and Dataset. To see the effect of the single time vector of the structure with time format, I like to plot the data using the stem function.

Simple model output

With the Dataset format, each signal is a timeseries object with its own sample time. Whereas for the Structure with Time, since there is only a single time vector, data has to be recorded for all outputs at every sample time.

Now its Your Turn

If you are logging simulation data using Structure with Time, I recommend using the Dataset format instead as it is more flexible and gives you independent time vectors. This format also allows you to log units information, you can log directly to a MAT-file, and can even log multiple values for a single time step, which is useful inside iterator subsystems.

Give it a try and let us know what you think by leaving a comment here.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.