bio_img_simulink

Guy on Simulink

Simulink & Model-Based Design

Extracting Timetables… Simulink style

Today I want to share a simple tip to interact with data logged from a Simulink model

The Problem

Let's take this simple model that logs 3 signals with different sample times:
After simulating, it is possible to use extractTimetable to create a single timetable with all the logged signals:
out = sim('testLog');
TT = out.logsout.extractTimetable
TT = 11×3 timetable
TimesineTs1sineTs2sineTs3
10 sec000
21 sec0.0628NaNNaN
32 sec0.12530.1253NaN
43 sec0.1874NaN0.1874
54 sec0.24870.2487NaN
65 sec0.3090NaNNaN
76 sec0.36810.36810.3681
87 sec0.4258NaNNaN
98 sec0.48180.4818NaN
109 sec0.5358NaN0.5358
1110 sec0.58780.5878NaN
What's the problem? In one word: NaN.
By default, extractTimetable inserts NaN for steps where signals with a sample time slower than the fastest rate did not execute. While this is technically correct because there was no data point at this time, I find this not ideal for data logged from Simulink.
In Simulink, if I was to connect a Rate Transition block and other blocks downstream from this logged signal, they would not receive NaNs. Instead, the previous value would be held.
Those NaNs also make plots look strange. Here is the data as seen in the Simulation Data Inspector:
When plotting the timetable, the NaNs cause gaps in the signals:
stackedplot(TT);

The Solution

To obtain a timetable that I find better aligned with Simulink's behavior, you can first extract the data as a cell array that contains one timetable per signal, and then combine them using the synchronize function:
TT = out.logsout.extractTimetable(OutputFormat="cell-by-signal");
TT = synchronize(TT{:},"union","previous")
TT = 11×3 timetable
TimesineTs1sineTs2sineTs3
10 sec000
21 sec0.062800
32 sec0.12530.12530
43 sec0.18740.12530.1874
54 sec0.24870.24870.1874
65 sec0.30900.24870.1874
76 sec0.36810.36810.3681
87 sec0.42580.36810.3681
98 sec0.48180.48180.3681
109 sec0.53580.48180.5358
1110 sec0.58780.58780.5358
This looks better.
stackedplot(TT);

Now it's your turn

Do you prefer the default behavior of extractTimetable? Would you prefer if the "zero-order hold" behavior shown in this post was the default? Let us know in the comments below.

|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.