Using timetable versus tscollection objects

9 views (last 30 days)
Alan Bindemann
Alan Bindemann on 16 Nov 2020
Commented: Alan Bindemann on 23 Nov 2020
I have Simulink models that log signal data using the Simulink.SimulationData.Dataset class.
I'm writing post-processing m-code that helps analyze model outputs and would like to combine the data into either a tscollection object or a timetable object, which is easier to deal with than the collection of Simulink.SimulationData.Signal objects contained in the logsout object generated by the Simulink model.
There doesn't seem to be an 'easy' way to convert the signals in logsout into a timetable object, whereas one can create a tscolleciton object rather easily:
tsCol = tscollection;
for ii=1:logsout.numElements
tsCol = tsCol.addts(logsout.getElement(ii).Values);
MathWorks seems to really be pushing the use of timetables through functions such as stackedplot, and tscollection objects seem to be something of an afterthought.
It there an easy way to convert a tscollection object into a timetable? Failing that, is there a way to create an empty timetable and add the signal data from the Dataset signals in a loop as shown above?

Answers (1)

Corey Silva
Corey Silva on 20 Nov 2020
Hi Alan,
Below is a toy example where you have a timeseriescollection containing two timeseries, ts1 and ts2.
>> ts1 = timeseries(rand(5,1),'Name','ts1');
>> ts2 = timeseries(rand(5,1),'Name','ts2');
>> tsc = tscollection({ts1,ts2});
>> tt = timetable(seconds(tsc.Time),tsc.ts1.Data(:),tsc.ts2.Data(:),'VariableNames',{'ts1_Data','ts2_Data'})
tt =
5×2 timetable
Time ts1_Data ts2_Data
_____ ________ ________
0 sec 0.15761 0.14189
1 sec 0.97059 0.42176
2 sec 0.95717 0.91574
3 sec 0.48538 0.79221
4 sec 0.80028 0.95949
Hopefully this gives you enough to get started with using timetables. I would like to assure you that we are actively working on solutions to converting between timeseries objects and timetables for future releases of MATLAB.
Alan Bindemann
Alan Bindemann on 23 Nov 2020
Thanks, That helps a lot. The code below is close to doing what I want.
for ii=1:logsout.numElements
elemTs = logsout.getElement(ii).Values;
if ~isempty(elemTs.DataInfo.Units)
Units{ii} = elemTs.DataInfo.Units.Name;
Units{ii} = '';
Names{ii} = elemTs.Name;
if ii==1
tTable = timetable(seconds(elemTs.Time), elemTs.Data);
tTable = addvars(tTable, squeeze(elemTs.Data));
tTable.Properties.VariableUnits = Units;
tTable.Properties.VariableNames = Names;
I recommend that the MathWorks mentions the addvars and removevars functions in the documentation for timetable.
Alan Bindemann

Sign in to comment.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by